Difference between char *argv[] and char **argv for the second argument to main()

58,673

Solution 1

It is fundamental to c that char** x and char* x[] are two ways of expressing the same thing. Both declare that the parameter receives a pointer to an array of pointers. Recall that you can always write:

 char *parray[100];
 char **x;

 x = &parray[0];

and then use x identically.

Solution 2

Basically, char* argv[] means array of char pointers, whereas char** argv means pointer to a char pointer.

In any array, the name of the array is a pointer to first element of the array, that is, it contains the address of the first element.

So in the code given below, in char array x, x is a pointer to first element, '1', which is a character. So it's pointer to a character.

And in array arr, arr is pointer first element, x, which is itself a pointer to a character. So it a pointer to another pointer.

Hence, x is char*, and arr is char**.

While receiving something in a function, basic rule is that, you have to tell the type of the thing you are receiving. So either you simply say that you want to receive a char**, or you can also say char* arr[].

In first case, we don't need to think anything complex. We simply know, we are receiving an array of char*. Don't we know this? So, we receive it, and use it.

In second case, it is simple, as i have explained above that arr is a char**, you can put this as it's type and receive it safely. Now the system knows the type of the stuff we have received, we can access next elements by simply using array annotation. It's like, we have received the starting address of the array, we can surely go to the next elements, and as we know it's type, we know what it contains and how we can use that further. We know it contains pointer to char, so we can legally access them as well.

void func1(char* arr[])
{
    //function body
}
void func2(char** arr)
{
    //function body
}

int main()
{
    //x, y and z are pointer to char
    char x[3]={'1', '2', '3'};
    char y[3]={'4', '5', '6'};
    char z[3]={'7', '8', '9'};

    //arr is pointer to char pointer
    char* arr[3]={x, y, z};

    func1(arr);
    func2(arr);
}

Solution 3

[EDIT] Using GCC at the time of the comment probably GCC 7.2

declaring an array like this

char array[]

makes it const which means that you CAN'T have the following code

char array[] = "hello";
array = "hey";

even though the second string is smaller and should fit you get this error

error: array type 'char [6]' is not assignable

if you have **argv you can write

main(int argc, char **argv)
{
    char **other_array;
    /*
     * do stuff with other_array
     */
    argv = other_array;
}

if you have *argv[] then

main(int argc, char *argv[])
{
    char **other_array;
    /*
     * do stuff with other_array
     */
    argv = other_array;
}

gives you this warning

warning: assigning to 'const char **' from 'char **' discards qualifiers in nested pointer types

so it is technically a small optimisation as if you had written const

Share:
58,673
Jhansi Rani
Author by

Jhansi Rani

Updated on September 17, 2020

Comments

  • Jhansi Rani
    Jhansi Rani over 3 years

    CODE 1

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

    CODE 2

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

    CODE 1 and CODE 2 both give same output. but argument 2 of main function in CODE 1 and CODE 2 are different. Array of pointers are created above data section at compile time. argv is array of pointers. Then we should declare argument in main function as pointer to pointer to character i.e., **argv. How it is correct to declare as in CODE 1?