How do I create a string array? I know that the individual strings are 8 characters long, but I don't know how many of them I will have...
thnx,
Squirrelly1
Printable View
How do I create a string array? I know that the individual strings are 8 characters long, but I don't know how many of them I will have...
thnx,
Squirrelly1
Use a:
Something like that.Code:// 8 characters long - dont know how many
char *myString[8];
// know how many - stored in intHoweverMany!
*myString = (char**)malloc(sizeof(char[8]) * intHoweverMany);
HD
One syntax error. And don't forget to free that. And it should be char[9] for the terminating NUL.
C++:
Better C++:Code:// 8 characters long - dont know how many
char *myString[9];
// know how many - stored in intHoweverMany!
myString = new char[9][intHoweverMany];
// ...
delete[] myString;
And if the number of strings could change afterwards (or you have to just read string by string and don't know when you come to the end):Code:#include <string>
using namespace std;
string *myString;
myString = new string[numStrings];
// ...
delete[] myString;
Code:// This line is only for MSVC++
#pragma warning(disable: 4786)
#include <vector>
#include <string>
using namespace std;
typedef vector<string> strvec;
strvec myString;
// e.g.
myString.push_back("Hello, World!");