store argv[1] to an char variable
Asked Answered
H

5

5

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

Hashum answered 15/1, 2014 at 11:54 Comment(4)
"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).Jacquelynnjacquenetta
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).Defend
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.Hashum
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.Defend
P
14

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

}
Parke answered 15/1, 2014 at 11:57 Comment(0)
K
3

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.

Killjoy answered 15/1, 2014 at 11:57 Comment(0)
S
2

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.

Screeching answered 15/1, 2014 at 11:57 Comment(0)
I
1

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]

Impunity answered 15/1, 2014 at 11:58 Comment(0)
N
0

You would need the strcpy function found in the string.h header. It will loop through your string and stop when it finds the '\0'. Otherwise, you would have to loop through with argv[n][n] and copy every letter to your variable until you find the '\0'.

Nadiya answered 4/6 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.