Solved

char * address to char array

Posted on 2001-07-14
4
358 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 50 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

786 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