Lew Pitcher
4/29/2011 12:06:00 AM
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