Hello there, guys!
I am relatively new to programming in C, and I still have a lot to learn. I am currently using Dev C++, but I work in C only. My problem is that I need to fill a 2D table with some multi-character variables, and I don't know how to declare one. I declared the table as char, but only the it was initiated only to the first value I gave, not all of them. I mean, if I put "HELLO", it would contain only "H".
I was thinking, would it be wise to use a 3D table, and use its third dimension as the maximum character length of the strings I want to use? I know this would entail a lot more work, and I hope there is another way. If anyone can help me, please do so! I will be forever grateful!
Please, reply ASAP!
- ShadowHawk

This works:

int main(int argc, char *argv[])
{
	char table[5][10]={"Hello","Hello","Hello","Hello","Hello"};
	int i=0;
	for(i=0;i<5;i++)
	{
		printf("%d %s\n",i,table[i]);
	}
	return 0;
}

Also, don't compile C under a C++ compiler, if you can avoid it,
you will get some odd results.

>I hope there is another way

There is! It's called the Standard Template Library. Using a vector of strings (both vector and string are STL classes) would do the trick very nicely. If you need to use native char arrays, then you could do this:

const int MAXSTRINGLENGTH = 256; //or whatever you want the maximum string length to be
const int MAXNUMBEROFSTRINGS = 10; //or whatever you want the maximum number of strings to be
char myArrayOfStrings[MAXIMUMNUMBEROFSTRINGS][MAXIMUMSTRINGLENGTH];

This article has been dead for over six months. Start a new discussion instead.