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

Recommended Answers

All 3 Replies

This works:

int main(int argc, char *argv[])
	char table[5][10]={"Hello","Hello","Hello","Hello","Hello"};
	int i=0;
		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

The STL is fantastic, but C does not support it. It looks like the OP wants to work in C only.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.