1

Speaking of C
I know that

void main (int argc, char *argv[])

is the correct way to pass arguments to main,
but out of curiosity i wrote

void main (int argc, char *argv[1])

and the program, after compilation showed the exact same result as previous one.
What exactly i did in second version, can somebody explain me that?
thanks in advance.

user3694243
  • 234
  • 3
  • 13

3 Answers3

7
void main (int argc, char *argv[1])

and

void main (int argc, char *argv[])

are equivalent.

argv is a pointer (char**) and the size specified for it in main() is not the actual size of strings in argv -- because an array passed to a function gets converted into a pointer to its first element. Basically the size value is ignored by the compiler.

For the same reason, you can specify:

void main (int argc, char *argv[101])

and it will still work as you'd expect. It can be confusing for anyone reading the code. But it's perfectly valid.

Relevant post: What is array decaying?

Community
  • 1
  • 1
P.P
  • 117,907
  • 20
  • 175
  • 238
  • A small correction. the size value **of the last dimension** is ignored by the compiler. No difference in this case, but in the sake of preciseness might worth a fix. – Shachar Shemesh May 26 '16 at 11:51
  • @ShacharShemesh that is not strictly true. In the function `foo(int array[][16])` it is the last dimension that the compiler *does* need to know. – Weather Vane May 26 '16 at 11:55
  • 1
    It'd be unambiguous to say *leftmost dimension is ignored*. – P.P May 26 '16 at 11:56
  • is it up to compiler to allow such declaration? – user3694243 May 26 '16 at 21:39
  • @user3694243 This is *decaying* is guaranteed by C standard and so, compiler obliges (not specific to any particular compiler/platform). – P.P May 27 '16 at 07:24
0

First, speaking of C, return type of main() should be int. This is from the C language standard:

5.1.2.2.1 Program startup

1. The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

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

or equivalent; or in some other implementation-defined manner.

Second, char *argv[] is there to allow for multiple command line arguments. Although char *argv[0] looks strange but valid, it is common to leave to your command line argument parser dealing with this programmatically.
Here is a code example demonstrating that char *argv[0] does not affect command line argument parsing:

#include <stdio.h>

int main(int argc, char *argv[0])
{
    for (int i = 0; i < argc; i++)
        printf("%s\n", argv[i]);    
    return 0;
}
user3078414
  • 1,942
  • 2
  • 16
  • 24
0

In the context of a function parameter declaration, T a[N] and T a[] are both equivalent to T *a; they declare a as a pointer to T.

In the case of argv, T is char *.

If T is an array type R [M], then R a[N][M] and R a[][M] are equivalent to R (*a)[M]; they declare a as a pointer to an array of R.

John Bode
  • 119,563
  • 19
  • 122
  • 198