?
Solved

char * address to char array

Posted on 2001-07-14
4
Medium Priority
?
391 Views
Last Modified: 2011-10-03
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.

0
Comment
Question by:havman56
4 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6282284
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...
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 6282352
In C/C++ there is an implicit conversion from an array of some type  (T[]) to a pointer to that type (T*) that points to the first element of the array.   Because fo this, you can specify an array of soem type to any function that expects a pointer to that type.

So in this case you need a pointer to char * in order to call strcpy(), but you can specify an array of char (char[]) and it will be implicitly converted to a pointer to the first character in the array..
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6283086
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
0
 
LVL 4

Author Comment

by:havman56
ID: 6301334
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question