Click here to Skip to main content
14,209,192 members
Rate this:
Please Sign up or sign in to vote.
See more:
How can we assign char* to char**??

For example:
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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
char ** strArray = (char **)malloc(N * sizeof(char*));

Then you could use an assignment such as
strArray[1] = str;  // save the address of this string to strArray
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Yes....I made a terrible mistake there. Thanx Pallini.
   
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)



Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190617.3 | Last Updated 4 May 2010
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100