about argc and argv

Discussion in 'C Programming' started by vicky, Jun 27, 2007.

  1. vicky

    vicky Guest

    hi all,

    please tell me with example, how the *argv[] point to the the no of
    strings.
     
    vicky, Jun 27, 2007
    #1
    1. Advertising

  2. vicky

    Lew Pitcher Guest

    On Jun 27, 3:16 pm, vicky <> wrote:
    > hi all,
    >
    > please tell me with example, how the *argv[] point to the the no of
    > strings.


    Your question doesn't make much sense. You seem to be asking for an
    example of how *argv[] points to the number of argv strings. Well, it
    doesn't.

    However, given a main() function prototype of
    int main(int argc, char *argv[]);
    then, upon initial entry into main(),
    argc will have been set to the count of the number of strings that
    argv will point to, and
    argv will have been set to point to an array of pointers to the
    individual strings

    Thus,
    argv[0] will point to the "program name" string, what ever that is,
    argv[1] will point to the first argument string,
    argv[2] will point to the second argument string,
    and so on, with
    argv[argc-1] pointing to the last argument string, and
    argv[argc] pointing at a NULL pointer

    So,
    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char *argv[])
    {
    if (argc < 0)
    printf("Failure: argc is negative, and that's not allowed\n");
    else
    {
    if (argc == 0)
    printf("Success: argc is zero, so there are no argv[] strings
    passed to main()\n");
    else
    {
    int arg;
    for (arg = 0; arg < argc; ++arg)
    if (argv[arg] != NULL)
    printf("Success: argv[%d] points to \"%s
    \"\n",arg,argv[arg]);
    else
    printf("Huh????: argv[%d] points to a NULL\n",arg);
    }
    if (argv[argc] == NULL)
    printf("Success: the last argv[] points to NULL\n");
    else
    printf("Failure: the last argv[] points to something, not NULL
    \n");
    }
    return EXIT_SUCCESS;
    }

    will enumerate the string parameters passed through argv

    HTH
    --
    Lew
     
    Lew Pitcher, Jun 27, 2007
    #2
    1. Advertising

  3. "vicky" <> wrote in message
    news:...
    > hi all,
    >
    > please tell me with example, how the *argv[] point to the the no of
    > strings.
    >


    Here's a simple program to echo arguements to the user.

    #include <stdio.h>

    int main(int argc, char **argv)
    {
    int i;

    for(i=0;i<argc;i++)
    printf("***%s***\n", argv);
    return 0;

    }

    If you are asking how the argv array is set up, main() isn't the real entry
    point to the program. There is some startup-code which queries the
    command-line interface for the input, and sets up strings for main() to
    read. However it is very platform-specific and normally you never need to
    know about it.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Jun 27, 2007
    #3
  4. vicky

    John Gordon Guest

    In <> vicky <> writes:

    > please tell me with example, how the *argv[] point to the the no of
    > strings.


    argv is an array of character strings.
    argc indicates how many entries the array contains.

    So, for example, if you wanted to display the first string, you could
    do it like this:

    printf("%s\n", argv[0]);

    (Remember, C arrays are indexed starting at zero.)

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Jun 27, 2007
    #4
  5. vicky

    Eric Sosman Guest

    John Gordon wrote On 06/27/07 16:38,:
    > In <> vicky <> writes:
    >
    >
    >>please tell me with example, how the *argv[] point to the the no of
    >>strings.

    >
    >
    > argv is an array of character strings.


    Almost: argv is an array of pointers, and those
    pointers point to the beginnings of character strings.

    --
     
    Eric Sosman, Jun 27, 2007
    #5
  6. In article <>,
    Malcolm McLean <> wrote:

    >If you are asking how the argv array is set up, main() isn't the real entry
    >point to the program. There is some startup-code which queries the
    >command-line interface for the input, and sets up strings for main() to
    >read. However it is very platform-specific and normally you never need to
    >know about it.


    That's one possible implementation, but not the only one. main()
    can indeed be "the real entry point to the program", if the
    operating system's method of invoking the program so cooperates.
    But having main() as the "real entry point" complicates using
    main recursively.

    If main() is implemented as "the real entry point", there is also
    a bit of complication if an exit handler is registered. It often
    makes implementation sense for there to be something that main()
    can [at an implementation level] "return to", that does the
    exit handler work. But that's only a practical matter, not
    how the DS9K will handle it between 3:14 and 3:17 tomorrow afternoon.
    --
    Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
     
    Walter Roberson, Jun 27, 2007
    #6
  7. Eric Sosman said:

    > John Gordon wrote On 06/27/07 16:38,:
    >> In <> vicky
    >> <> writes:
    >>
    >>
    >>>please tell me with example, how the *argv[] point to the the no of
    >>>strings.

    >>
    >>
    >> argv is an array of character strings.

    >
    > Almost: argv is an array of pointers, and those
    > pointers point to the beginnings of character strings.


    Almost: argv is a pointer to the first element in an array of pointers.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jun 27, 2007
    #7
  8. Eric Sosman <> writes:
    > John Gordon wrote On 06/27/07 16:38,:
    >> In <> vicky
    >> <> writes:
    >>>please tell me with example, how the *argv[] point to the the no of
    >>>strings.

    >>
    >>
    >> argv is an array of character strings.

    >
    > Almost: argv is an array of pointers, and those
    > pointers point to the beginnings of character strings.


    Almost. argv is a pointer to pointer to char. It points to the first
    element of an array of pointers to char. Each element of that array
    either is a null pointer, or points to a string (i.e., points to the
    first character of a string).

    argv can be declared as "char *argv[]", but that really means "char
    **argv" (a rule that applies only to parameter declarations).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 27, 2007
    #8
  9. vicky

    Eric Sosman Guest

    Richard Heathfield wrote:
    > Eric Sosman said:
    >
    >> John Gordon wrote On 06/27/07 16:38,:
    >>> In <> vicky
    >>> <> writes:
    >>>
    >>>
    >>>> please tell me with example, how the *argv[] point to the the no of
    >>>> strings.
    >>>
    >>> argv is an array of character strings.

    >> Almost: argv is an array of pointers, and those
    >> pointers point to the beginnings of character strings.

    >
    > Almost: argv is a pointer to the first element in an array of pointers.


    Y'know, I thought to myself as I wrote it, "Self," I
    thought, "some pedant is going to pedantize you for this.
    How very pedantestrian."

    As a function argument, an array is all but indistinguishable
    from a pointer. For an explication of "all but," see the FAQ or
    see http://math.boisestate.edu/GaS/patience/webop/pat16d.html .

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 28, 2007
    #9
  10. On Wed, 27 Jun 2007 12:36:03 -0700, Lew Pitcher
    <> wrote:

    >On Jun 27, 3:16 pm, vicky <> wrote:
    >> hi all,
    >>
    >> please tell me with example, how the *argv[] point to the the no of
    >> strings.

    >
    >Your question doesn't make much sense. You seem to be asking for an
    >example of how *argv[] points to the number of argv strings. Well, it
    >doesn't.
    >
    >However, given a main() function prototype of
    > int main(int argc, char *argv[]);
    >then, upon initial entry into main(),
    > argc will have been set to the count of the number of strings that
    >argv will point to, and
    > argv will have been set to point to an array of pointers to the
    >individual strings
    >
    >Thus,
    > argv[0] will point to the "program name" string, what ever that is,
    > argv[1] will point to the first argument string,
    > argv[2] will point to the second argument string,
    >and so on, with
    > argv[argc-1] pointing to the last argument string, and
    > argv[argc] pointing at a NULL pointer


    Just a nit since your code handles it correctly. argv[argc] doesn't
    point to a NULL pointer; it is a NULL pointer. Or, it you prefer, it
    is a pointer that has been set to NULL.



    Remove del for email
     
    Barry Schwarz, Jun 28, 2007
    #10
  11. Barry Schwarz <> writes:
    [...]
    > Just a nit since your code handles it correctly. argv[argc] doesn't
    > point to a NULL pointer; it is a NULL pointer. Or, it you prefer, it
    > is a pointer that has been set to NULL.


    Meta-nit: it's a null pointer, not a NULL pointer.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 28, 2007
    #11
  12. Eric Sosman said:
    > Richard Heathfield wrote:
    >> Eric Sosman said:
    >>> John Gordon wrote On 06/27/07 16:38,:
    >>>>
    >>>> argv is an array of character strings.
    >>> Almost: argv is an array of pointers, and those
    >>> pointers point to the beginnings of character strings.

    >>
    >> Almost: argv is a pointer to the first element in an array of
    >> pointers.

    >
    > Y'know, I thought to myself as I wrote it, "Self," I
    > thought, "some pedant is going to pedantize you for this.
    > How very pedantestrian."


    Y'know, you were right.

    > As a function argument, an array is all but indistinguishable
    > from a pointer.


    Very true, but we are discussing argv, which is not an argument but a
    parameter. As function parameters, arrays and pointers are trivial to
    distinguish. Basically, if it's a parameter, it can't be an array.
    Easy.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jun 28, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. userfriendly

    argc and argv

    userfriendly, Feb 29, 2004, in forum: C++
    Replies:
    3
    Views:
    6,012
    Buster
    Mar 1, 2004
  2. Claudio Varini

    use of argc and argv

    Claudio Varini, Oct 28, 2004, in forum: C++
    Replies:
    3
    Views:
    2,149
    Howard
    Oct 28, 2004
  3. Billy Patton

    How to alter argc and argv

    Billy Patton, May 31, 2005, in forum: C++
    Replies:
    5
    Views:
    861
    red floyd
    Jun 3, 2005
  4. Hal Styli
    Replies:
    14
    Views:
    1,657
    Old Wolf
    Jan 20, 2004
  5. ern
    Replies:
    2
    Views:
    431
    Walter Roberson
    Aug 17, 2006
Loading...

Share This Page