By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 981 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

dynamic string assigning?

P: 2
Hi, I would like to dynamic assigning different sets of strings to display on screen, such as,

if user choose 1, display "1111", "111111", "111",
if user choose 2, display "222", "22222", "2222", "22",
if user chooses 3, then "333", "33"

Is the following declaration OK?

char *str[][3]={{"1111", "111111", "111"}, {"222", "22222", "2222", "22"}, {"333", "33"}};

seems compiler always waring that

warning: initialization from incompatible pointer type
warning: excess elements in scalar initializer

how could I solve this problem? thanks in advance
Jul 12 '06 #1
Share this Question
Share on Google+
4 Replies


P: 35
If I take the above declaration as such then the obvious error is that the declaration of the str size is wrong. You have taken strings that are having around 6 characters whereas in the declaration you have declared the size as 3. This is why the compiler is throwing errors.
If this is not what you wanted to know then pls elobrate your question becoz what I understood was this. One more thing, its working fine on my system.
Jul 12 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
Not quite right Ashish_CPP, there is no string size problem because the actual data type is char *, but you are along the right lines.

The problem with

char *str[][3]={{"1111", "111111", "111"}, {"222", "22222", "2222", "22"}, {"333", "33"}};

is that you have delared an unspecified size array to an array of 3 char *, however in the 22222 structure you have not 3 but 4 strings (pointers to char *) and since 4 > 3 you get

warning: excess elements in scalar initializer

This can probably be fixed by changing the declaration to

char *str[][4]={{"1111", "111111", "111"}, {"222", "22222", "2222", "22"}, {"333", "33"}};

however this has a small gotcha because the 1st and 3rd entries in the array still contain 4 pointers but you have only provided 3 and 2 initialisers respectively. The entries that you have not provided initialisers for (str[0][3] for instance) will have the value NULL.
Jul 12 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
warning: initialization from incompatible pointer type

It is possible that on your system string constants are of type const char *, this is sometimes true in which case char * and const char * are not the same causing this message.
Jul 12 '06 #4

P: 2
thanks! but what if in the following case?

there are conditions that might have different selections for user, such as

char *format_1[]={"aaaa", "bbbb", "cc", "ddd"};
char *format_2[]={"111", "2222", "33333", "444", "5555"};
..
if user choose 1, I have to count how many items in the string list and show the contents of format_1; 2, list format_2... etc.

This is a simple example which can be achieved by 1 if-else statement, but what if there are 10 cases?

can I just use an array to save these strings and do things just like getting array entries from array index to get the whole format_1 or so?

Is there any better way to declare such cases?

Thanks for any kindly answer and help... :)
Jul 13 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.