Link to home
Start Free TrialLog in
Avatar of havman56
havman56

asked on

char * address to char array

I want to copy a address of char* buffer to char array.

(pStMessage_t->m_bufferData) = (pMessage->GetData());

here pMessage->GetData() returns char*.
pStMessage_t->m_bufferData is array.

how do i get address of (pMessage->GetData()) point to address array.

Avatar of jhance
jhance

char * and char [] are usually interchangeable.  So for example:

char lpszString[32];
const char *lpszConstString = "This is a test";

strcpy(lpszString, lpszConstString);

is legal.  So in your case, you should be able to do:

char *buffer = pMessage->GetData();
buffer = pStMessage_t->m_bufferData;

Now buffer points to the string...
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi havman56
If the pStMessage_t->m_bufferData declared as char[] then you CAN'T simply do something like "pStMessage_t->m_bufferData = pMessage->GetData()". Actually "char[]" is the same as "char * const" (note the "const" keyword, it means that you pointer is const). This means for you that you can change the data inside the array but you can not reference to another buffer by this variable. Every C/C++ book says that in this situation you should copy content of the buffer (e.g. using strcpy()) instead of copying pointers. Also please look at the help for pMessage->GetData() and find out who should free the memory, returned pointer refers to.

Let me know if you have further questions about it.

Pavlik
Avatar of havman56

ASKER

i am very much thanksful for all the answers i agree with all ones.
but i feel nietod given some theory based on his answers so i am accepting as answer.

jhance and pavlik also given right one.
thanks for immed response from all