Argv question

C

Chad

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?
 
B

Ben Pfaff

Chad said:
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.)
 
C

Chad

Chad said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top