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.