[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

I think argv[i] should give us a pointer value. Why it is given the value pointed by pointer

tahir rauf

4/28/2011 8:16:00 PM

Hi GURUS,

I have some confusion about char *argv[], My understanding about it as
follows

1. I think that argv is an char pointer array. Each char pointer in
the array points to some strings.

argv
-------- +-----+ +---+---
+---+---+---+---+------+
| . | ======>
-------- | *: | *======> | F | i |
r | s | t | '\0' |

+-----+ +---+---
+---+---+---+---+------+

+-----+ +---+---
+---+---+---+---+------+

| *: | *======> | S | e
| c | o | n | d | '\0' |

+-----+ +---+---
+---+---+---+---+------+

2. When I write argv[0], I should get a pointer value.

3. If I write the *(argv[0]), it will give me the the word 'F'.

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.

Regards


Regards
12 Answers

Ben Bacarisse

4/28/2011 8:49:00 PM

0

tahir rauf <tahir.rauf1@gmail.com> writes:

> I have some confusion about char *argv[], My understanding about it as
> follows
>
> 1. I think that argv is an char pointer array.

It's a pointer not an array. It points to the start of an array so it
behaves very much like one, bit it is a pointer none the less.

> Each char pointer in
> the array points to some strings.

argv pointer to the first of an array of character pointers. Each char
pointer (except for the last one) points to the start of a single
string. The last one, argv[argc] is guaranteed to be a null pointer.

> argv
> -------- +-----+ +---+---
> +---+---+---+---+------+
> | . | ======>
> -------- | *: | *======> | F | i |
> r | s | t | '\0' |
>
> +-----+ +---+---
> +---+---+---+---+------+
>
> +-----+ +---+---
> +---+---+---+---+------+
>
> | *: | *======> | S | e
> | c | o | n | d | '\0' |
>
> +-----+ +---+---
> +---+---+---+---+------+

The wrapping has confused this but it looks good except for the missing
NULL as the end. Note that you got it more right then your text. It's
clear from the diagram that argv is a pointer, not an array.

> 2. When I write argv[0], I should get a pointer value.

Yup.

> 3. If I write the *(argv[0]), it will give me the the word 'F'.

'F' is not a word, but a byte. Also, note that argc might be zero in
which case argv[0] is null and you can't validly write *argv[0].

> 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.

argv[0] is indeed a pointer It points to the 'F'. If you treat it as a
string (say by writing printf("%s\n", argv[0]);) you will get all of the
string that starts with the 'F'. I.e. the pointer value is often
considered to be synonymous with the string to whose start it points.

If this does not explains matters, please say what you mean by "it
returns me the 'First'" and/or why you think you are not getting a
pointer value, perhaps by showing some code that is confusing you.

--
Ben.

Lew Pitcher

4/28/2011 8:53:00 PM

0

On April 28, 2011 16:16, in comp.lang.c, tahir.rauf1@gmail.com wrote:

> Hi GURUS,
>
> I have some confusion about char *argv[], My understanding about it as
> follows

> 1. I think that argv is an char pointer array.

Correct

> Each char pointer in the array points to some strings.

Maybe, maybe not.

Rather: Each argv[] is a character pointer. Valid character pointers point
at a character. This character /may/ be followed by others, accessable as
offsets from the character pointer. The character pointer /may/ point to
such an array of characters, which /may/ include the end-of-string
terminating character \0

If *argv[] is used as the second argument of main() (as in
int main(int argc, char *argv[]),
then each of the argv[] elements from argv[0] to argv[argc-1] points to
the first character of unique arrays of characters, with each array
terminated by an end-of-string character, such that each of these argv[]
elements may be treated as strings in logic that manipulate such. The
final argv[] (argv[argc]) is NULL, and does not point to any character or
array of characters.

[snip ascii art]
> 2. When I write argv[0], I should get a pointer value.

Correct


> 3. If I write the *(argv[0]), it will give me the the word 'F'.

Correction: If your code executes *(argv[0]), it will retrieve the first
character pointed to by argv[0], which (in your example, snipped, above)
would be the character 'F'.

Given
int main(int argc, char *argv[]),
argv is an array of pointers to char,
argv[0] is a pointer to char, and
*argv[0] is a char

> My Confusion: I am just wondering that when I write the argv[0], it
> returns me the 'First'.

No, it doesn't.

argv[0] references a pointer. It does not reference a sequence of
characters.

> Whereas I am expecting a pointer value. Can any one please help me in
> clarifying this concept.

It is likely that you use the pointer to retrieve characters, rather than
using the pointer as a value unto itself.

Perhaps you
printf("%s",argv[0]);
The "%s" argument tells printf to accept a pointer-to-char, and dereference
it into a sequence of characters, terminated by an end-of-string character;
the argv[0] resolves into a pointer-to-char.

If you
printf("%p",argv[0]);
then the "%p" argument tells printf to accept a pointer, and display it in a
platform-specific manner; again, the argv[0] resolves into a
pointer-to-char.

HTH
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfr... | Just Linux: http://jus...
---------- Slackware - Because I know what I'm doing. ------


Angel

4/28/2011 9:06:00 PM

0

On 2011-04-28, tahir rauf <tahir.rauf1@gmail.com> wrote:
>
> 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.

argv[0] is a string, and it contains the name of the executable file.
*argv[0] is a character value, containing the first character of the
name of the executable file.

Your confusion is probably that you don't realize that in C, strings are
pointers. C doesn't actually have a 'string' data type. 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.


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.)

To see the actual pointer value, do this:

printf("p", argv[0]);

This isn't very interesting though as it will just print a very long
hexadecimal number.


--
The perfected state of a spam server is a smoking crater.
- The Crater Corollary to Rule #4

Chad

4/28/2011 9:36:00 PM

0

On Apr 28, 1:52 pm, Lew Pitcher <lpitc...@teksavvy.com> wrote:
> On April 28, 2011 16:16, in comp.lang.c, tahir.ra...@gmail.com wrote:
>
> > Hi GURUS,
>
> > I have some confusion about char *argv[], My understanding about it as
> > follows
> > 1. I think that argv is an char pointer array.
>
> Correct
>
> > Each char pointer in the array points to some strings.
>
> Maybe, maybe not.
>
> Rather: Each argv[] is a character pointer. Valid character pointers point
> at a character. This character /may/ be followed by others, accessable as
> offsets from the character pointer. The character pointer /may/ point to
> such an array of characters, which /may/ include the end-of-string
> terminating character \0
>
> If *argv[] is used as the second argument of main() (as in
>   int main(int argc, char *argv[]),
> then each of the argv[] elements   from argv[0] to argv[argc-1] points to
> the first character of unique   arrays of characters, with each array
> terminated by an end-of-string   character, such that each of these argv[]
> elements may be treated as   strings in logic that manipulate such. The
> final argv[] (argv[argc]) is   NULL, and does not point to any character or
> array of characters.
>
> [snip ascii art]
>
> > 2. When I write argv[0], I should get a pointer value.
>
> Correct
>
> > 3. If I write the *(argv[0]), it will give me the the word 'F'.
>
> Correction: If your code executes *(argv[0]), it will retrieve the first
> character pointed to by argv[0], which (in your example, snipped, above)
> would be the character 'F'.
>
> Given
>   int main(int argc, char *argv[]),
> argv     is an array of pointers to char,
> argv[0]  is a pointer to char, and
> *argv[0] is a char
>
> > My Confusion: I am just wondering that when I write the argv[0], it
> > returns me the 'First'.
>
> No, it doesn't.
>
> argv[0] references a pointer. It does not reference a sequence of
> characters.
>
> > Whereas I am expecting a pointer value. Can any one please help me in
> > clarifying this concept.
>
> It is likely that you use the pointer to retrieve characters, rather than
> using the pointer as a value unto itself.
>
> Perhaps you
>   printf("%s",argv[0]);
> The "%s" argument tells printf to accept a pointer-to-char, and dereference
> it into a sequence of characters, terminated by an end-of-string character;
> the argv[0] resolves into a pointer-to-char.
>

Does the type pointer-to-char get bound to the variable or compile
time? Or is it implementation defined?

Chad

Lew Pitcher

4/29/2011 12:06:00 AM

0

On Apr 28, 5:35 pm, Chad <cdal...@gmail.com> wrote:
> On Apr 28, 1:52 pm, Lew Pitcher <lpitc...@teksavvy.com> wrote:
>
>
>
> > On April 28, 2011 16:16, in comp.lang.c, tahir.ra...@gmail.com wrote:
>
> > > Hi GURUS,
>
> > > I have some confusion about char *argv[], My understanding about it as
> > > follows
> > > 1. I think that argv is an char pointer array.
>
> > Correct
>
> > > Each char pointer in the array points to some strings.
>
> > Maybe, maybe not.
>
> > Rather: Each argv[] is a character pointer. Valid character pointers point
> > at a character. This character /may/ be followed by others, accessable as
> > offsets from the character pointer. The character pointer /may/ point to
> > such an array of characters, which /may/ include the end-of-string
> > terminating character \0
>
> > If *argv[] is used as the second argument of main() (as in
> >   int main(int argc, char *argv[]),
> > then each of the argv[] elements   from argv[0] to argv[argc-1] points to
> > the first character of unique   arrays of characters, with each array
> > terminated by an end-of-string   character, such that each of these argv[]
> > elements may be treated as   strings in logic that manipulate such. The
> > final argv[] (argv[argc]) is   NULL, and does not point to any character or
> > array of characters.
>
> > [snip ascii art]
>
> > > 2. When I write argv[0], I should get a pointer value.
>
> > Correct
>
> > > 3. If I write the *(argv[0]), it will give me the the word 'F'.
>
> > Correction: If your code executes *(argv[0]), it will retrieve the first
> > character pointed to by argv[0], which (in your example, snipped, above)
> > would be the character 'F'.
>
> > Given
> >   int main(int argc, char *argv[]),
> > argv     is an array of pointers to char,
> > argv[0]  is a pointer to char, and
> > *argv[0] is a char
>
> > > My Confusion: I am just wondering that when I write the argv[0], it
> > > returns me the 'First'.
>
> > No, it doesn't.
>
> > argv[0] references a pointer. It does not reference a sequence of
> > characters.
>
> > > Whereas I am expecting a pointer value. Can any one please help me in
> > > clarifying this concept.
>
> > It is likely that you use the pointer to retrieve characters, rather than
> > using the pointer as a value unto itself.
>
> > Perhaps you
> >   printf("%s",argv[0]);
> > The "%s" argument tells printf to accept a pointer-to-char, and dereference
> > it into a sequence of characters, terminated by an end-of-string character;
> > the argv[0] resolves into a pointer-to-char.
>
> Does the type pointer-to-char get bound to the variable or compile
> time? Or is it implementation defined?

Given the function argument
char *argv[]
then
argv is an array of pointer-to-char, with this type bound at compile
time


Chad

4/29/2011 12:37:00 AM

0

> > > Perhaps you
> > >   printf("%s",argv[0]);
> > > The "%s" argument tells printf to accept a pointer-to-char, and dereference
> > > it into a sequence of characters, terminated by an end-of-string character;
> > > the argv[0] resolves into a pointer-to-char.
>
> > Does the type pointer-to-char get bound to the variable or compile
> > time? Or is it implementation defined?
>
> Given the function argument
>   char *argv[]
> then
>   argv is an array of pointer-to-char, with this type bound at compile
> time- Hide quoted text -
>

Ughhh....I meant to write "bound to the variable at compile time?".
I'm glad you were able to see past the typo and still answer the
question.

Ben Bacarisse

4/29/2011 12:57:00 AM

0

Angel <angel+news@spamcop.net> writes:

> On 2011-04-28, tahir rauf <tahir.rauf1@gmail.com> wrote:
>>
>> 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.

It may be a bit fussy, but if this declaration appears in the parameter
list of a function (as the OP intended) then argv is not at array but a
pointer. In effect, []s turn into another * when the declarator is in a
function heading.

It does make a difference. argv can be assigned to (an array can't be
assigned to) and sizeof argv will not tell you anything about the size
of the array of program argument 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.
>
> argv[0] is a string, and it contains the name of the executable file.
> *argv[0] is a character value, containing the first character of the
> name of the executable file.

....or it will be a null character if the name of the program is not
available to OS.

<snip>
--
Ben.

Keith Thompson

4/29/2011 6:49:00 AM

0

Angel <angel+news@spamcop.net> writes:
> On 2011-04-28, tahir rauf <tahir.rauf1@gmail.com> wrote:
>>
>> 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-f....

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Angel

4/29/2011 7:11:00 AM

0

On 2011-04-29, Keith Thompson <kst-u@mib.org> wrote:
> Angel <angel+news@spamcop.net> writes:
>> On 2011-04-28, tahir rauf <tahir.rauf1@gmail.com> wrote:
>>>
>>> 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.
<snipped rest for brevity>
>
> Recommended reading: section 6 of the comp.lang.c FAQ,<http://www.c-f....

Keith, you are of course absolutely correct (I assume any way, didn't
actually check). What I wrote was meant to be a simple explaination of
the confusion, not a syntactically correct and standards-compliant
description of the language. :-)

The concepts of strings, arrays and pointers in C, and the various ways
they interact are very confusing to the beginning programmer (I'm
inclined to say they can confuse intermediate programmers as well), so I
didn't want to burden this poster with the actual definitions (I
deliberately left out the description of strings as arrays of
characters, and the interaction between arrays and pointers), but just
with a simple explaination of what the *argv[] argument to the main()
function is and does. Perhaps I kept it too simple, I don't know.

Anyway, thank you for your additions and corrections. ^^


--
The perfected state of a spam server is a smoking crater.
- The Crater Corollary to Rule #4

Seebs

4/29/2011 10:42:00 PM

0

On 2011-04-28, Angel <angel+news@spamcop.net> wrote:
> On 2011-04-28, tahir rauf <tahir.rauf1@gmail.com> wrote:
>> 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.

Not quite! At least one member of the array of pointers is NULL, and is
not a string.

> Your confusion is probably that you don't realize that in C, strings are
> pointers. C doesn't actually have a 'string' data type. 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.

However, not all pointers are strings. While string isn't a data type in
the sense of showing up in the type checking system, it is a defined thing,
and not all pointers to characters are strings.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.