Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 432
Jhansi Rani Avatar asked Nov 30 '14 13:11

Jhansi Rani


People also ask

Is it char * argv or char * argv?

char** argv is the same as char* argv[] because in the second case "the name of the array is a pointer to the first element in the array".

What does char * argv [] mean in C?

The declaration char *argv[] is an array (of undetermined size) of pointers to char , in other words an array of strings. And all arrays decays to pointers, and so you can use an array as a pointer (just like you can use a pointer as an array).

What does * argv [] mean?

As a concept, ARGV is a convention in programming that goes back (at least) to the C language. It refers to the “argument vector,” which is basically a variable that contains the arguments passed to a program through the command line.

What does int argc char * argv [] mean in C?

Command-line Arguments: main( int argc, char *argv[] ) Here argc means argument count and argument vector. The first argument is the number of parameters passed plus one to include the name of the program that was executed to get those process running.


2 Answers

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.

like image 121
bmargulies Avatar answered Sep 26 '22 05:09

bmargulies


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); } 
like image 39
nishantbhardwaj2002 Avatar answered Sep 26 '22 05:09

nishantbhardwaj2002