Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can we assign char* to char**??

For example:
C
char ** strArray = (char **)malloc(N*sizeof(char));
char *str ;
std::string st ("Hi there how are you??");
str = new char[st.size()+1];
strcpy(str, st.c_str());

strArray[1] = ??? <-----str (copy str contents to strArray)

Regards,

Abhishek Dey
Posted
Updated 4-May-10 10:22am
v3

You have defined strArray as an array of char* (i.e. pointers to character strings) but your allocation is of N * the size of a single character. I suspect what you meant was
C++
char ** strArray = (char **)malloc(N * sizeof(char*));

Then you could use an assignment such as
C++
strArray[1] = str;  // save the address of this string to strArray
 
Share this answer
 
Yes....I made a terrible mistake there. Thanx Pallini.
 
Share this answer
 
Comments
HimanshuJoshi 5-May-10 2:51am    
Instead of adding answer. Please add a comment next time you want to thank somebody

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900