Need immediate help on casting/storing values in buffer

I have a structure type, let's say Telephone

1
2
3
4
5
6
typedef struct
{
  string dir_no;
  unsigned char msg_ref;

}Telephone;


//I create a pointer to it and store values.
1
2
3
4
Telephone* tele;

tele->dir_no = "123";
tele->msg_ref = //some value; 



Now I want to pass this structure to a function, let's say RegisterNumber.

unsigned int ret_value = RegisterNumber(//some params...).

However,I cannot pass tele structure directly because RegisterNumber accepts some other structure type.
So I need to typecast "tele" to the structure, lets say, storeBuffer, that RegisterNumber() accepts.

This structure, storeBuffer is defined as

1
2
3
4
5
typedef struct 
{
	char p_buffer[200]; 
	 
}storeBuffer, 


So in order for program to work, I basically need to store entire tele structure into p_char buffer and typecast it.

My O.S is 64 bit and I am not sure how would I do it? Could anybody please help?
I am thinking something like that but not sure whether it will work or not.

1
2
storeBuffer* lp_store_buffer;
lp_store_buffer->p_buffer = (storeBuffer*) &(tele);


Thanks in advance. I hope I am clear in explanation.

It's going to have to be a bit more complicated than that, potentially. The string class contains, tucked away somewhere, at least one char pointer that is pointing to some other memory somewhere, which contains the char array that is the actual data. If all you do is take a copy of that pointer, then you will lose that data as soon as the string goes out of scope, or you'll change the string without realising that this also affects the storeBuffer (cf. "deep copy" vs "shallow copy").

This being the mighty C++, you certainly can just insist to your code that it treat an object of type Telephone as if it were something else. However, I expect you can do a bit better. This being C++, you can write your own conversion function. Oh yes.

Looks to me like your storeBuffer object is just 200 bytes of memory ready to go. So how about something like this (I've not checked all the indirections and what have you, but it should be good enough to explain itself):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool TelephoneIntoBuffer(Telephone* input, storeBuffer* output)
{
  if (input->dir_no.length() > 199)
  {
     // Too big to store! 
     return false; // failure
  }

  output->pbuffer[0] = input->msg_ref; // first char stores msg_ref
  // remaining chars store the chars of the string
  memcpy(&(output->pbuffer[1]), input->dir_no, input->dir_no.length());

  return true; // success
}


Now, the data is stored neatly and when you want to transform it back, you can do so.

Also, this is C++. typedef struct is no longer necessary - just struct.

One final thing - I just want to check that you realise that this code
storeBuffer* lp_store_buffer; does NOT create an object of type storeBuffer, so this:

1
2
storeBuffer* lp_store_buffer;
lp_store_buffer->p_buffer = something;
is wrong and will probably cause a segFault. I think you do know this, but since you're creating the structs in a C way rather than a C++ way, it's worth checking that you do.
Last edited on
Thanks a lot Moschops for a quick reply. Actually the structure is much complicated but I cannot post code here. But you have given a right directions on further proceeding. I think this will do :) Thanks a lot again for your help :)
Topic archived. No new replies allowed.