|
From: Brenden T. <br...@rc...> - 2002-12-26 20:22:59
|
Balazs is correct. However, there are a few other ways to do this. For
example, the C standard says that command line parameters will be passed
into main as a count, and as an array of pointers to stings, just as
Balazs said:
int main( int argc, char *argv[] ) {... }
argc is the ARGument Count, and argv is the ARGument Vector. However,
argv has one other little trick. It actually has argc + 1 vectors, and
the last vector is always NULL, so you can just ignore argc and count
through the strings untill you find the NULL one, and that is the end.
Obviously, this wouldn't work if some of your strings in the middle
have to be NULL, so watch out, but if they don't then it's a nice
alternative to having to maintain a seperate count, although a little
tricky to maintain. See K&R section "Command Line Arguments" more info
and nice pictures.
Also, you could have structure that has the count in the beginning and
then a variable sized char* vector after it. I believe gcc allows you
to declare variable sized vectors like this:
struct string_vect{
int count;
char *string[0];
} string_vect;
Then you have just one variable to pass into functions instead of two.
Again the maintainace cost is a little higher (you have to remeber to
allocate memory for this structure properly, i.e. malloc( sizeof (struct
string_vect) + sizeof (char* [10])) for ten strings for example, but
perhaps this would be better in the long run for you.
Good luck!
B.
P.S. I did NOT compile and check any of the code example given here.
Use at your own risk!
Khalid El-Kary wrote:
> Hi,
> I'm a newbie, just wanted to ask about the best way to make an array
> of strings (without using a string class) i mean using pointers "char *".
>
> I tried to make a pointer of pointer "char **" but i found that i'm
> unable to count the number of elements in this kind of work.
>
> Any help will be appreciated, thanx
>
> Regrads
> Khalid Al-kary
> http://creaturesx.ma.cx/kxparse/
>
>
> _________________________________________________________________
> Add photos to your e-mail with MSN 8. Get 3 months FREE*.
> http://join.msn.com/?page=features/featuredemail&xAPID=42&PS=47575&PI=7324&DI=7474&SU=
> http://www.hotmail.msn.com/cgi-bin/getmsg&HL=1216hotmailtaglines_addphotos_3mf
>
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
|