Solved

char * address to char array

Posted on 2001-07-14
4
353 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now