store argv[1] to an char variable

55,259

Solution 1

If you look at the declaration of main() you see that it's

int main(int argc, const char **argv);

or

int main(int argc, const char *argv[]);

So argv is an array of const char * (i.e. character pointers or "C strings"). If you dereference argv[1] you'll get:

"s"

or:

{ 's' , '\0' }

and if you dereference argv[1][0], you'll get:

's'

As a side note, there is no need to copy that character from argv[1], you could simply do:

const char *myarg = NULL;

int main(int argc, const char **argv) {
    if (argc != 2) {
        fprintf(stderr, "usage: myprog myarg\n");
        return 1;
    } else if (strlen(argv[1]) != 1) {
        fprintf(stderr, "Invalid argument '%s'\n", argv[1]);
        return 2;
    }

    myarg = argv[1];

    // Use argument as myarg[0] from now on

}

Solution 2

argv is an array of strings, or say, an array of char *. So the type of argv[1] is char *, and the type of argv[1][0] is char.

Solution 3

The typical declaration for argv is

char* argv[]

That is an array of char*. Now char* itself is, here, a pointer to a null-terminated array of char.

So, argv[1] is of type char*, which is an array. So you need another application of the [] operator to get an element of that array, of type char.

Solution 4

Lets see mains' signature:

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

No, argv is not a one-dimensional array. Its is a two-dimensional char array.

  1. argv[1] returns char*

  2. argv[1][0] returns the first char in in argv[1]

Share:
55,259
yaylitzis
Author by

yaylitzis

Updated on September 17, 2021

Comments

  • yaylitzis
    yaylitzis almost 3 years

    I pass a character to my program and I want to store this character to variable. For example I run my program like this ./a.out s file. Now I want to save the argv[1] (its the s) to a variable (lets say I define it like this char ch;. I saw this approach:

    ch = argv[1][0];
    

    and it works. But I cant understand it. The array argv isnt a one dimensional array? if i remove the [0], I get a warning warning: assignment makes integer from pointer without a cast

    • Admin
      Admin over 10 years
      "The array argv isnt a one dimensional array?" - no: char *argv[] -- it's a pointer-to-pointer. (You can think of it as a pointer to the first element of an array of pointers-to-char).
    • hyde
      hyde over 10 years
      In C, difference between an array and a pointer may seem a bit hazy. Pointer can always be interpreted as pointer to first element of array and indexed with [], and argv[1] gives pointer to char, so another [] can be added to use it as char array (which is what it really is in this case, too).
    • yaylitzis
      yaylitzis over 10 years
      Ok i understood it. I read this crasseux.com/books/ctutorial/argc-and-argv.html and it says that argv is a one-dimensional array of strings. thats why I thought it was a one dimensional.
    • hyde
      hyde over 10 years
      C has two kinds of arrays of strings: 1-dimensional array of char pointers (each pointing to array of char, can even be same one, and pointer can be NULL too), and 2-dimensional array of char (each row is string with max length of row size). Array of pointers is more common, though.
  • Rishabh Agarwal
    Rishabh Agarwal almost 5 years
    what? it doesn't make sense
  • Rishabh Agarwal
    Rishabh Agarwal almost 5 years
    how will you convert char * argv[] to char variable and char [] variable
  • trojanfoe
    trojanfoe almost 5 years
    @RishabhAgarwal Start a new question.