471,092 Members | 1,101 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

dynamic string assigning?

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
4 3485
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
9,065 Expert Mod 8TB
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
9,065 Expert Mod 8TB
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
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.

Similar topics

5 posts views Thread by gtz669 | last post: by
16 posts views Thread by drj0nson | last post: by
23 posts views Thread by Russ Chinoy | last post: by
6 posts views Thread by Paul | last post: by
21 posts views Thread by arnuld | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.