CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

help for c language

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Marc
Guest







help for c language
PostPosted: Tue Jul 17, 2007 2:10 pm     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Jul 17, 2007 6:10 pm     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Tue Jul 17, 2007 7:45 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jul 17, 2007 8:50 pm     Reply with quote

There is an example of a 2-dimensional const array in the link below.
Look about halfway down the post, at the declaration for:
"char const weekday_names[7][10]"
http://www.ccsinfo.com/forum/viewtopic.php?t=27988
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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