injoy
injoy

Reputation: 4373

What is the meaning of argv[1][0]?

I am reading codes of mcachefs and could not understand some codes, like below:

printf("mcachefs " __MCACHEFS_VERSION__ " starting up...\n");

if (argc == 1 || argv[1][0] == '-')
{
    fprintf(stderr,
        "\tError : first argument shall be the the mcachefs_mountpoint !\n");
    exit(2);
}

I have two questions: 1: For printf("mcachefs " __MCACHEFS_VERSION__ " starting up...\n"), is it the correct way to use printf? I have never seen such way of using it.

  1. What is the meaning of argv[1][0]? I know argv[] represents the arguments from the command line. But, is not it a one dimension array?

Upvotes: 2

Views: 18139

Answers (7)

Scooter
Scooter

Reputation: 7061

argv is a one-dimensional array. It's elements are character pointers. So argv[i] gives a pointer to a char in memory. The values in argv point to the first character of the program name (as a c null terminated "string") and any additional arguments passed to the program.

argv[0] -------> my_program
argv[1] -------> -some_arg

To get the character that is pointed to by an argv element you can use the dereference operator:

*argv[1]

or, C also allows array syntax to dereference a pointer:
argv[1][0] /* dereference the pointer stored in argv[1] */

It may be clearer if the character pointer is used outside the argv array:

char *arg1 = argv[1]

printf("first char of the first arg is %c\n",*arg1);  /* dereference char pointer */
printf("another way to access first char of %c\n",arg1[0]); /* dereference char pointer */

Upvotes: 1

nhahtdh
nhahtdh

Reputation: 56809

For the first question, it is very likely that __MCACHEFS_VERSION__ is macro for a string literal. In C, string literals are concatenated if there is nothing between them.

For the second question, argv[1][0] refers to the first character of the first argument passed into the function (e.g. if you type a command myprogram -la then myprogram can be accessed from argv[0], and -la will be in argv[1]). argv[0] is the name of the executable, so the arguments to the executable will be from index 1.

The code therefore checks whether argc == 1, which means no argument passed in, or argv[1][0] == '-', which means the first argument is something like a flag, and return error. The logic here is: we check for the number of arguments first, and if the number of arguments is not 1 (short circuit effect of logical operator), then we check for the first argument.

Upvotes: 2

sashang
sashang

Reputation: 12194

argv[1][0] refers to the 1st character of the 2nd string.

The expression

if (argc == 1 || argv[1][0])

relies on the languages short circuit evaluation to safe-gaurd the array dereference.In other words if the the test argc == 1 fails then the subsequent expression argv[1][0] is not evalauted. If it was then the derefence may cause a segfault.

The expression in the printf relies on C languages support for the concatenation of string literals. The macro __MCACHEFS_VERSION__ will be #defined as as string literal somewhere. It allows you to split strings across new lines, or use them in macros as shown in your example. See here for more examples of string literal concatenation: http://en.wikipedia.org/wiki/C_syntax#String_literal_concatenation

Upvotes: 1

jarmond
jarmond

Reputation: 1382

  1. __MCACHEFS_VERSION__ is a macro for a string literal, e.g. "1.0". When multiple string literals are placed adjacently they are concatenated: "Version:" __MCACHEFS_VERSION__ "..." would become "Version: 1.0..." in this case.

  2. argv[1][0] refers to the first element of second element. It is equivalent to:

    const char* firstArg = argv[1];

    char firstCharOfFirstArg = firstArg[0];

Upvotes: 1

user23743
user23743

Reputation:

  1. When the C compiler sees a lot of string literals next to each other, it concatenates them into one long string. So that use of printf() is OK, as long as the macro __MCACHEFS_VERSION__ expands to a string. That string had better not contain any percent characters... I'd write it as

    printf("mcachefs %s starting up...\n", __MCACHEFS_VERSION__);

  2. As others have pointed out, argv[1] is the second string in the string array argv, and strings are character arrays so argv[1][0] is the first character in the second string.

Upvotes: 2

awatts
awatts

Reputation: 1067

This would determine if the first character of the first parameter is a dash.

So for

command -firstparam

(argc == 1 || argv[1][0] == '-') would evaluate to true.

Upvotes: 1

ouah
ouah

Reputation: 145829

It means the first character of the second string of argv.

Upvotes: 1

Related Questions