I read this in a book, do you think it correct, or just a typing mistake?
hint: double asterisks
char **argv
argv: pointer to char
I read this in a book, do you think it correct, or just a typing mistake?
hint: double asterisks
char **argv
argv: pointer to char
The code is right. The description is not. argv is an array of pointer to char.
The comment doesn't match the code.Originally Posted by thinhare
Emmanuel Delahaye
"C is a sharp tool"
so what's the right comment?
To be completely accurate, it should say it's a pointer to a pointer of type char. Or more simply, a pointer to a string.
frankly speaking, I think it's more likely the upper line which has been mis-typed. what do you think? if this is the case, how would you correct it to fit the description beneath?
(it is from the book written by Brian W. Kernighan and Dennis M. Ritchie.)
Please quote the edition and page/line number.Originally Posted by thinhare
Emmanuel Delahaye
"C is a sharp tool"
I've proven that both the code and the comment are correct.
It does print a CHAR. What do you think?#include <stdio.h>
main(int argc, char *argv[]) {
printf("%c\n", **argv);
}
Emmanuel Delaha:
"The C Programming Language, Second Edition"
Chapter 5.12 - "Complicated Declarations"
It's correct, but it prooves thatOriginally Posted by thinhare
**argv is a char
*argv is pointer to char (char *)
argv is a pointer to a pointer to char (char **)
Please give the line number (I don't have the book at home). The errata list is here:"The C Programming Language, Second Edition"
Chapter 5.12 - "Complicated Declarations"
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html
Last edited by Emmanuel Delaha; 12-24-2004 at 12:39 PM.
Emmanuel Delahaye
"C is a sharp tool"
what do you find in the same book of yours?
I'd thought it was
which was mistyped with one more asterisk.char *argv
but considering it's in the section of complicated declarations, that is, something got to be complicated than usual, I became not so certain.
argv is whatever you want it to be....by convention it is typically declared in main's parameter list as char **argv or *argv[].
**argv, argv[0][0], and *argv[0] are all the same....(for the sake of this discussion).
argv[1][0] refers to the second string of the parameter list - first character.
argv[1][5] refers to the fifth character - second string of the parameter list.
**argv refers to argv[0][0] -- first string, first character
argv[1][0] refers to the second string of the parameter list.
*argv[0] = **argv = argv[0][0]
*argv[2] refers to the third character of the first string
all of the above can have the value NULL
one thing i don't know however
((argv+1) == &argv[1][0] ) = (true || false)?
Last edited by misplaced; 12-24-2004 at 01:05 PM.
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
Ok to understand this better, and I'm not sure why no one brought this up yet, here is what is being done:
Example:
Ok now compile and run this code. Ok it didn't do a whole lot. Now try something different like (calling the binary program in my examples):Code:#include <stdio.h> int main(int argc, char **argv) { int i; for(i = 0; i < argc; i++) printf("command line argument %02d: \"%s\"", i, argv[i]); return 0; }
Then how about:Code:program arg1 arg2 arg3
Ok now you will notice that it outputs each space separated string as a command line arg. Just for fun:Code:program hello world. this is a pointless program
That time you will find that it output the entire quote encased string as one argument. Anyway the point is that in the above argv is going to be an unknown sized array of strings. The size of the array will be known at run-time. That is what argc indicates. But as far as compile time there is no determined size. Why use char **? Because remember that we are not using an unknown lengthed string, we are using an array of strings that has an unpredetermined size. So instead of having something like this:Code:program "hello world. this is a pointless program"
Example:
We have something more like this:Code:char argv[] = { 'a', 'b', 'c' };
Example:
Code:char *argv[] = { "program", "arg1", "arg2" };
>"The C Programming Language, Second Edition"
>Chapter 5.12 - "Complicated Declarations"
It turns out to be a typing mistake, but on your part, not the book's. My copy of K&R2 says this:
Code:char **argv argv: pointer to pointer to char
My best code is written with the delete key.
He he! My point exactly!Originally Posted by Prelude
Emmanuel Delahaye
"C is a sharp tool"
OK. I got it. Thank you all!