Link to home
Create AccountLog in
Avatar of zizi21
zizi21

asked on

Urgent - Command line arguments

hi,
Lets say i have a command line argument like this :
add -this -add
now , i understand that
argv[0] = add
argv[1]= -this
argv[2]= -add

my problem is that i need to count the strlen of argv[1] without minus
i thought the answer was like this : strlen(argv[1] - 1)  /* i minus one because i took total of -this which is 5-1) but i found out this is wrong...

the right way to do it is :

strlen(argv[1]+1) /*Why plus one*/
or strlen(&argv[1][1]) /*I thought , this would only give strlen 1 coz argv[1][1] = refers to t...

thanks a lot
Avatar of slyong
slyong

You might mean:

strlen(argv[1])-1 /* the length of "-this" (which is 5) minus 1 gives 4

For the rest, strlen() takes a pointer as the argument and count until it arrives at a null character.  So:

strlen(argv[1]+1) points to the 't' of "-this" argv[1] is a pointer to '-' in "-this".. so this will count from 't' until the end which is basically the length of "this".

strlen(&argv[1][1]), argv[1][1] is the character 't', & operator gives you the memory address to it.  So again, strlen counts from the memory address of 't' until it meets a null character, which is basically the end of "this".
Avatar of zizi21

ASKER

I am a bit confused....
I thought strlen doesn't count NULL character...

ASKER CERTIFIED SOLUTION
Avatar of slyong
slyong

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of zizi21

ASKER

thanks a lot