Solved

char * address to char array

Posted on 2001-07-14
4
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
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.

691 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