Comparison with pointer to pointer to char, and char
Author |
Message |
Walnu #1 / 8
|
 Comparison with pointer to pointer to char, and char
Where argp is a pointer to a pointer to a char... char **argp; ... If (**argp == "\0") { } Can someone explain why this is not correct. I believe **argp to be the same as writing "z", or whatever it finally points to. Do I need to index here to meet some syntax rule? Thanks.
|
Fri, 02 Apr 2004 04:38:42 GMT |
|
 |
Ben Pfaf #2 / 8
|
 Comparison with pointer to pointer to char, and char
Quote:
> Where argp is a pointer to a pointer to a char... > char **argp; > ... > If (**argp == "\0") > { > }
argp is a pointer to a pointer to char. *argp is a pointer to char. **argp is a char. "\0" is a 2-element array of nonmodifiable char, which is converted to a pointer to char. Do you see the problem? Quote: > Can someone explain why this is not correct. I believe **argp to be the > same as writing "z", or whatever it finally points to. Do I need to index > here to meet some syntax rule?
You need to distinguish a string (e.g., "z") from a character (e.g., 'z'). -- "What is appropriate for the master is not appropriate for the novice. You must understand the Tao before transcending structure." --The Tao of Programming
|
Fri, 02 Apr 2004 04:52:48 GMT |
|
 |
Richard Heathfiel #3 / 8
|
 Comparison with pointer to pointer to char, and char
Quote:
> Where argp is a pointer to a pointer to a char... > char **argp; > ...
I assume (rightly? wrongly?) that ... means "initialise argp in some valid way. Quote: > If (**argp == "\0") > { > } > Can someone explain why this is not correct.
**argp is a char, so the above, which compares it to a static array of two char converted to a char * for comparison purposes, is wrong. If you want to compare argp[0] with the empty string, use strcmp(argp, "") == 0, and if you want to test whether argp[0][0] is the null character, then use either **argp == '\0' (note the single quotes) or argp[0][0] == '\0'. Quote: > I believe **argp to be the > same as writing "z", or whatever it finally points to.
Nope. if argp is char **, then *argp is char *, and **argp is a char, not a string. Quote: > Do I need to index > here to meet some syntax rule?
You need to read up on pointers. :-) --
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton
|
Fri, 02 Apr 2004 05:12:52 GMT |
|
 |
Lawrence Kir #4 / 8
|
 Comparison with pointer to pointer to char, and char
Quote:
>> Where argp is a pointer to a pointer to a char... >> char **argp; >> ... >I assume (rightly? wrongly?) that ... means "initialise argp in some >valid way. >> If (**argp == "\0") >> { >> } >> Can someone explain why this is not correct.
The first problem is that C is case sensitive so you need if rather than If. Quote: >**argp is a char, so the above, which compares it to a static array of >two char converted to a char * for comparison purposes, is wrong. If you >want to compare argp[0] with the empty string, use strcmp(argp, "") == >0,
Perhaps you meant strcmp(*argp, "") == 0 The question is what is argp supposed to be pointing to? Most likely it is to the first element of an array of pointers to strings, in which case: Quote: >and if you want to test whether argp[0][0] is the null character, >then use either **argp == '\0' (note the single quotes) or argp[0][0] == >'\0'.
Or to test whether the first pointer in the (assumed) array is a null pointer if (argp[0] == NULL) -- -----------------------------------------
-----------------------------------------
|
Fri, 02 Apr 2004 20:11:11 GMT |
|
 |
Richard Heathfiel #5 / 8
|
 Comparison with pointer to pointer to char, and char
Quote:
> >> Where argp is a pointer to a pointer to a char... > >> char **argp; > >> ... > >I assume (rightly? wrongly?) that ... means "initialise argp in some > >valid way. > >> If (**argp == "\0") > >> { > >> } > >> Can someone explain why this is not correct. > The first problem is that C is case sensitive so you need if rather than If.
Missed that. Quote: > >**argp is a char, so the above, which compares it to a static array of > >two char converted to a char * for comparison purposes, is wrong. If you > >want to compare argp[0] with the empty string, use strcmp(argp, "") == > >0, > Perhaps you meant strcmp(*argp, "") == 0
EEK! Yes, that's what I meant. --
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton
|
Fri, 02 Apr 2004 21:29:14 GMT |
|
 |
Walnu #6 / 8
|
 Comparison with pointer to pointer to char, and char
Shit....thanks guys. I haven't programmed C for a few weeks and I forgot that double quotes were arrays and single quotes gave the char value.
|
Sat, 03 Apr 2004 02:33:47 GMT |
|
 |
Default Use #7 / 8
|
 Comparison with pointer to pointer to char, and char
Quote:
> Shit....thanks guys. I haven't programmed C for a few weeks and I forgot > that double quotes were arrays and single quotes gave the char value.
You also forgot to quote what you are replying to, so those of us with newsreaders set to display only new messages know what you are talking about. Brian Rodenborn
|
Sat, 03 Apr 2004 04:33:41 GMT |
|
 |
Dan P #8 / 8
|
 Comparison with pointer to pointer to char, and char
Quote: >Where argp is a pointer to a pointer to a char... >char **argp; >... >If (**argp == "\0")
^^ Bzzzt! C keywords are lower case. You CANNOT write "If" instead of "if" in an if statement! Quote: > { > } >Can someone explain why this is not correct. I believe **argp to be the >same as writing "z", or whatever it finally points to. Do I need to index >here to meet some syntax rule?
You need to understand the difference between character constants and string literals. "\0" and "z" are string literals, but what you actually want is a character constant, i.e. '\0' or 'z'. The following code is correct (as far as the compiler is concerned, anyway): if (**argp == '\0') { /* ... */ } Dan -- Dan Pop CERN, IT Division
Mail: CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
|
Mon, 05 Apr 2004 22:38:37 GMT |
|
|
|