something like this will hold an unlimited number of strings that are of unlimited length.
const char *array[] = {
"jane",
"John Deer Company",
"The quick brown fox jumped over the lazy dog",
"Adam",
"Smith"
};
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Not a problem as such but am i asking too much....
I can do a dynamic array for a 'int' but not for a multi dimensional array, can anyone help us out here?
What is your intent -- what issue are you trying to solve?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
you can use malloc() to allocate the space needed to hold the characters
char *array2 = malloc( strlen(array[0]) +1 );
strcpy(array2, array[0]);
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
malloc() must be typecast when using a c++ compiler (file extension is *.cpp or *.cc), C compilers do not have that restriction.
char *array2 = reinterpret_cast<char*>(malloc( strlen(array[0]) +1 ));
Since you are writing c++ code, if you have the choice use std::string instead -- its a lot simpler.
std::string array2 = array[0];
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
You assign memory to buffer with the new statement which makes it capable of holding a string but you don't actually assign a string to buffer. Therefore buffer is empty, meaning it isn't a null terminated char array. I'm not sure what behaviour to expect when you send an empty char array to strlen which expects a null terminated char array. I suspect it is undefined behaviour and you compiler is returning a random value of 9.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396