0

I understand that command line arguments are pointed by the string pointers in the argv [] array. i.e. argv [0] contains a pointer to the name of the program and argv [1] a pointer to the very first command line argument.

But I noticed a very different application of argv on the internet and is written below. I have compiled it successfully and it print out the second arguments third character.

#include <stdio.h>  
    int main(int argc , char *argv []) {        
        printf("%c\n", argv [1][2]);                
        return 0; } 

I have two question arising from this use.

What is the true dimension of argv? does it stop at two or is there more?

I have noticed it the code that the programer has used %c to access and print the pointed content. Now, we can always parse a pointer (a string pointer) to %s and expect it to print out the set of chars untill null terminator.

upto the best of my understanding %c can only be used if the array it self contains the character.

i.e.

char yadhavan [] = yadhavan is a boy;
 printf ("%c", yadhavan [3]);

Could someone please explain how %c has been used with a pointer array?

A little more explanation is always more than welcome!

Richard Schwartz
  • 14,463
  • 2
  • 23
  • 41
Denis
  • 97
  • 9

3 Answers3

3

argv is of type char*[] and holds several strings(command line arguments).
argv[i] is of type char* and holds a string.
argv[i][j] is of type char and holds a character.

What is the true dimension of argv? Does it stop at two or is there more?

It depends on how many command line arguments there are.

Could someone please explain how %c has been used with a pointer array?

One can declare char pointers and make them point to strings or just use a char array and hold the string:

char* str="string";

Or

char str[]="string";

In both of these ways it is possible to refer a character using:

printf("%c",str[3]);

argv is actually an array of char*. So, it is possible to refer a single character from it.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • how does querying argv [i] [j] suddenly make the pointer array behave like a char array? – Denis Feb 08 '15 at 06:13
  • `argv[i][j]` isn't a pointer or a `char` array. It is a `char`. – Spikatrix Feb 08 '15 at 06:18
  • but argv was defined by me as a pointer array in the beginning of my code. How did it become a char array? im a little confused here. – Denis Feb 08 '15 at 06:21
  • Read [this](http://www.stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c) – Spikatrix Feb 08 '15 at 07:08
2

What is the true dimension of argv? does it stop at two or is there more?

argc tells you the argument count (or the dimension of argv as you put it). In other words, the number of strings in argv is equal to argc. So, argv can contain more than two strings if multiple arguments are provided. For example, if I were to execute some command like

$ foo file1 file2 ...

argv would look like

argv[0] = "foo\0"
argv[1] = "file1\0"
argv[2] = "file2\0"
...

See this other answer for more details on argc vs argv.

Could someone please explain how %c has been used with a pointer array?

First, %s is used for printing strings. %c is used for printing a single character. Second, argv is an array of strings, and a string is simply an array of characters (char). So, argv is really an array of arrays of chars. So, when you see

printf("%c\n", argv[1][2]);

the code is printing out the third character of the second argument in argv. So, if I were to execute the command

$ foo bar

argc and argv would look like

argc = 2
argv[0] = "foo\0"
argv[1] = "bar\0"
argv[1][0] = 'b'
argv[1][1] = 'a'
argv[1][2] = 'r'
argv[1][3] = '\0' // null terminator
Community
  • 1
  • 1
Garrett Hyde
  • 5,409
  • 8
  • 49
  • 55
0

char *argv [] is array of pointers and this acts as a two dimention array, it can't contains more than 2D. So the data in 'argv' is very similar to 2D, but limitations are not specified. You can check in the below link:Limit on the number of arguments to main in C

#include <stdio.h>  
   int main(int argc , char *argv []) {        
    printf("%c\n", argv [1][2]);                
    return 0; } 

Here char *argv [] is very similar to

Char argv[][]  //with no argument limitation.
Community
  • 1
  • 1
Kiran Kumar Kotari
  • 1,078
  • 1
  • 10
  • 24