01 said:
argv[] is an array that contains many pointers.argv[0] is a pointer to
another char array. that char arry contains the name of this program(
if not null) . is it true?
I think this may be where you go wrong.
What do you mean when you write "argv[]"? What are the empty square
brackets supposed to mean? argv[], with the empty square brackets, is
not a legal expression (it may appear in some declarations, but we'll
leave that aside).
(We sometimes informally refer to a function named "func" as "func()"
to emphasize the fact that it's a function. We don't usually do this
for arrays.)
Given:
int main(int argc, char *argv[]) { /* ... */ }
there is an object (specifically a parameter) whose name is argv. It
is of type pointer-to-pointer-to-char. In spite of the [] syntax, no
array is declared here; in this context, "char *argv[]" means exactly
the same thing as "char **argv".
So argv is a pointer-to-pointer-to-char; it points to a
pointer-to-char. That pointer-to-char is (probably) the first element
of an array of several pointers-to-char; that array will have been
created at execution time by the environment before it invokes the
main program.
So argv is an expression, the name of an object, and its type is
pointer-to-pointer-to-char.
That means that *argv, or equivalently argv[0], is an expression of
type pointer-to-char; if argv is non-null, then *argv is a pointer to
the first character of a string containing some representation of the
program name.
so printf("%s", argv[0]) would print the contents of argv[0] which is
a pointer.
No. argv[0] is a pointer to char; it happens to point (probably) to
the first character of a string. printf() with "%s" doesn't print the
contents of the pointer; it prints the string that the pointer points
to. (More precisely, the pointer points to the first character of the
string, but the standard defines "pointing to a string" to mean
"pointing to the first character of a string".)
why not use printf("%s", *argv[0]) to print the real stuff?
Because it's illegal (actually it invokes undefined behavior).
argv[0] is a pointer-to-char, so *argv[0] (it groups as *(argv[0])) is
an expression of type char. It's probably the first character of the
program name. So this attempts to print just the first character of
the program name, but since it uses the wrong format, it won't work.
This:
printf("%c", *argv[0]);
or, equivalently:
printf("%c", **argv);
or, equivalently:
printf("%c", argv[0][0]);
or, equivalently:
putchar(**argv);
prints the first character of the program name.
I suggest reading section 6 of the comp.lang.c FAQ, at
<
http://www.c-faq.com/>.