3
int main( int argc, char ** argv ){
//code here
 return 0; }   

I know that:

  1. argc is indicates the number of command line arguments including the file name
  2. char ** argv is supposed to be a char* to an array which was initially represented as char* argv[]

Assuming I am right what is with the relatively new notation char **argv compared to char * argv[]? What does it point to?

I read this post Where are C/C++ main function's parameters? however it seems to explain where the arguments are and nothing else.

Community
  • 1
  • 1
Denson
  • 121
  • 1
  • 2
  • 11

2 Answers2

8

The main prototype with parameters in the C Standard is:

int main(int argc, char *argv[]) { ... }

Now in C a function parameter of type array is adjusted 1) to a type pointer, that is:

void foo(T param[])

is equivalent to

void foo(T *param)

so using char *argv[] or char **argv for the main parameter is exactly the same.


1) (C99, 6.7.5.3 Function declarators (including prototypes) p15) "[...] (In the determination of type compatibility and of a composite type, each parameter declared with function or array type is taken as having the adjusted type and each parameter declared with qualified type is taken as having the unqualified version of its declared type.)"
ouah
  • 142,963
  • 15
  • 272
  • 331
  • i read somewhere that in c the main function be called from anywhere in the program. is this true? how will it work? – Denson Jun 14 '14 at 08:00
3

In C/C++ you can't pass an array by value to a function. A pointer to array or its reference can be passed. When used as function parameter the array type is equivalent to pointer type, i.e

void foo(int arr[]);

is equivalent to

void foo(int *arr);

Similarly int *argv[] is equivalent to int **argv.
But note that this is true only when array types are used as parameter of a function otherwise array and pointers are two different type.

haccks
  • 104,019
  • 25
  • 176
  • 264