|
|
View previous topic :: View next topic |
Author |
Message |
Marc Guest
|
help for c language |
Posted: Tue Jul 17, 2007 2:10 pm |
|
|
I need to define an array of strings and to initialize it with certain strings
There is a sintax error. Which?
char *pan_ids[5];
pan_ids[5]={"aaaa", "bbbb", "cccc", "dddd", "eeee"};
Thank You |
|
|
Bill Boucher
Joined: 04 Feb 2005 Posts: 34 Location: Chatham,ON,CA
|
|
Posted: Tue Jul 17, 2007 6:10 pm |
|
|
I don't think an array or string variable can be a pointer (as indicated by preceding *).
Also, "pan_ids[5]" can only hold 1 string such as "aaaa" at a time. If you want it to hold 5 strings, then you need a multi-dimensional array such as "pan_ids [5] [5]".
Try
Code: | char pan_ids[5][5]={"aaaa", "bbbb", "cccc", "dddd", "eeee"};
|
If the strings are fixed (won't change) then stick "const" in your declaration to store the string data in program memory space instead of ram. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Tue Jul 17, 2007 7:45 pm |
|
|
Bill Boucher wrote: | I don't think an array or string variable can be a pointer (as indicated by preceding *). |
I think, Marc is trying to create an array of pointers to strings. There's no conceptual difference between an array variable and a pointer to the 1st member in the array.
Marc wrote: | Code: | pan_ids[5]={"aaaa", "bbbb", "cccc", "dddd", "eeee"}; |
|
This will not work for 3 reasons.
1. You are trying to assign the whole array to a single slot.
2. ... and this slot is out of bounds of the array. The largest index is four (4).
3. The assignment syntax array[]={<member list>} works only during the initialization of the array. Afterwards you need to assign each array memer individually. If only need to initialize the array, you can do this (note that C counts the members in the initialization list, so there is no number in the brackets):
Code: | char* pan_ids[] = {"aaaa", "bbbb", "cccc", "dddd", "eeee"}; |
And finally, please read Kernigan & Ritchie. You will do yourself a huge favor. http://www.cs.bell-labs.com/cm/cs/cbook/ _________________ Read the label, before opening a can of worms.
Last edited by kender on Wed Jul 18, 2007 7:11 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|