Angel said:
My Confusion: I am just wondering that when I write the argv[0], it
returns me the 'First'. Whereas I am expecting a pointer value. Can
any one please help me in clarifying this concept.
char *argv[] is an array of strings. Since a string in C is implemented
as a pointer to the first character of the string in memory, you can also
say that it is an array of pointers.
Um, no.
Since argv is a parameter, even though it appears to be declared
as an array, it's really a pointer. As a parameter declaration,
"char *argv[]" is equivalent to "char **argv". argv is a pointer to a
pointer to char.
argv[0] is a string, and it contains the name of the executable file.
No, argv[0] *points to* a string, where pointing to a string is a
shorthand for pointing to the first character of a string.
*argv[0] is a character value, containing the first character of the
name of the executable file.
Right, though it's not actually guaranteed that the string is the name
of the exectubale in any directly usable manner. The standard says that
the string, if it exists, "represents the program name".
Your confusion is probably that you don't realize that in C, strings are
pointers.
Your confusion is that in C, strings absolutely are not pointers.
Try this:
char *s = "hello, world";
printf("sizeof s = %d\n", (int)sizeof s);
printf("sizeof \"hello, world\" = %d\n", (int)sizeof "hello, world");
Quoting 7.1.1p1 of the C99 standard:
A _string_ is a contiguous sequence of characters terminated
by and including the first null character. [...] A _pointer
to a string_ is a pointer to its initial (lowest addressed)
character. The _length of a string_ is the number of bytes
preceding the null character and the _value of a string_ is
the sequence of the values of the contained characters, in order.
C doesn't actually have a 'string' data type.
Correct. A string is a data format, not a data type. Strings exist at
run time. A string is the content of an array.
Any library
function that works on strings accepts pointers as arguments, and if you
pass a pointer to such a function (for example the printf function,
which expects a string as its first argument), it will get treated as a
string.
A library function that works on strings takes (possibly qualified)
char* arguments. Each such argument is expected to be a pointer to a
string (i.e., it points to the first character of a string).
Part of the confusion is that C programs almost always access strings
via char* pointers; it doesn't have any facilities for operating
directly on strings themselves, as strings.
Another part is that, in several ways, the language deliberately makes
it look as if pointers an arrays were interchangeable. Array parameter
declarations are interpreted, at compile time, as pointer declarations.
An array values is implicitly converted, in most contexts, to a pointer
to its first element. (The exceptions are (1) the operand of sizeof,
(2) the operand of unary "&", the address operator, and (3) a string
literal in an initializer used to initialize an array object.)
So if you do this:
printf(argv[0]);
the result will be the name of the program. (This is actually a bad
example as the program name could contain characters that printf treats
specially, printf("%s", argv[0]; would be better.)
Right, but what's passed to printf is a pointer, not a string.
Internally, printf uses pointer arithmetic to traverse the characters
of the string.
To see the actual pointer value, do this:
printf("p", argv[0]);
You left out a character:
printf("%p", argv[0]);
And "%p" expects a void*, not a char*, so you should add a cast:
printf("%p", (void*)argv[0]);
(The language stops *just* short of guaranteeing that this will work
without the cast, but it's a good idea anyway; there are no such
guarantees for, for example, int*.)
This isn't very interesting though as it will just print a very long
hexadecimal number.
Typically 8 or 16 digits.
Recommended reading: section 6 of the comp.lang.c FAQ,<
http://www.c-faq.com>.