Argv question

Discussion in 'C Programming' started by Chad, Dec 27, 2009.

  1. Chad

    Chad Guest

    Given the following...
    #include <stdio.h>

    int main(int argc, char *argv[])
    {

    printf("argv[0] is: %s\n", argv[0]);
    printf("(*++argv)[0] is: %c\n", (*++argv)[0]);
    printf("argv[0] + 1 is: %s\n", (argv[0]+1));

    return 0;
    }

    This produces the following after I compile and run it.

    $ ./entab2 -10
    argv[0] is: ./entab2
    (*++argv)[0] is: -
    argv[0] + 1 is: 10

    How come, under the GNU debugger, I can get '-' the print if I do
    something like print *(argv)[0]. But I can't get the '1' to print when
    I do print *(argv)[1]?

    (gdb) print *(argv)
    $15 = 0xbfb7e9cf "-10"
    (gdb) print *(argv)[0]
    $16 = 45 '-'
    (gdb) print *(argv)[1]
    Cannot access memory at address 0x0
    (gdb)

    I mean, isn't "-10" just all one string?
    Chad, Dec 27, 2009
    #1
    1. Advertising

  2. Chad

    Ben Pfaff Guest

    Chad <> writes:

    > How come, under the GNU debugger, I can get '-' the print if I do
    > something like print *(argv)[0]. But I can't get the '1' to print when
    > I do print *(argv)[1]?


    *(argv)[1] is the same as *(argv[1]), but you want (*argv)[1].

    (In C, as well as in mathematics, there is rarely any point is
    putting parentheses around a single token, outside of macro
    interactions.)
    --
    Ben Pfaff
    http://benpfaff.org
    Ben Pfaff, Dec 27, 2009
    #2
    1. Advertising

  3. Chad

    Chad Guest

    On Dec 26, 5:49 pm, Ben Pfaff <> wrote:
    > Chad <> writes:
    > > How come, under the GNU debugger, I can get '-' the print if I do
    > > something like print *(argv)[0]. But I can't get the '1' to print when
    > > I do print *(argv)[1]?

    >
    > *(argv)[1] is the same as *(argv[1]), but you want (*argv)[1].
    >
    > (In C, as well as in mathematics, there is rarely any point is
    > putting parentheses around a single token, outside of macro
    > interactions.)
    > --


    Why doesn't the distinction matter for argv[0]? Here is what I mean..

    (gdb) print *(argv)[0]
    $14 = 45 '-'
    (gdb) print (*argv)[0]
    $15 = 45 '-'
    (gdb) print *(argv)[1]
    Cannot access memory at address 0x0
    (gdb) print (*argv)[1]
    $16 = 49 '1'
    (gdb)

    Both print *(argv)[0] and print (*argv)[0] (appear) to produce the
    same thing. However, print *(argv)[1] and (*argv)[1]. Why is this?
    Chad, Dec 27, 2009
    #3
    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. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,584
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    5,952
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,629
    Old Wolf
    Jan 20, 2004
  4. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    901
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  5. jab3

    char **argv & char *argv[]

    jab3, Dec 4, 2004, in forum: C Programming
    Replies:
    5
    Views:
    665
    Chris Torek
    Dec 8, 2004
Loading...

Share This Page