1

I have two different versions of codes using char array in C programming language.

First version:

int main(int argc, char *argv[]) {
    return 0;
}

Second version:

int main(int argc, char *argv) {
    return 0;
}

All codes compiling without warnings or errors, so there's any difference between this codes?

Julian
  • 1,592
  • 1
  • 13
  • 33
  • 1
    In the case of the `main()` function, the first is correct and the second is incorrect. (See [What should `main()` return in C and C++?](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) which also covers the arguments passed in as well as the return type). The difference is between pointer to some pointers to characters (first case) and a pointer to some characters (second case). Note that some compilers will complain (correctly) about the second case — that isn't a legitimate signature for `main()`. – Jonathan Leffler Dec 08 '18 at 10:17
  • 1
    second code section was wrong. – Julian Dec 08 '18 at 10:19
  • I recommend [this `main` function reference](https://en.cppreference.com/w/c/language/main_function). And probably a good book or tutorial about pointers and arrays. – Some programmer dude Dec 08 '18 at 10:20
  • 2
    The second should be `char **argv`. See https://stackoverflow.com/questions/4208444/c-difference-between-and –  Dec 08 '18 at 10:27
  • Please double check your question for "typos". – alk Dec 08 '18 at 10:32
  • In the context of defining function parameters this `char *argv[]` is 100% equivalent to `char **argv`. This holds for all types and all levels of indirection. So this `char argv[]` is equivalent to `char *argv`. – alk Dec 08 '18 at 10:35

2 Answers2

5

From C Standard#5.1.2.2.1p1

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;10) or in some other implementation-defined manner.

As per the standards, your second version is incorrect:

int main(int argc, char *argv) {
    return 0;
}

When compiling it with gcc compiler, I am getting following error message:

prg.c:1:5: error: second parameter of 'main' (argument array) must be of type 'char **'

Community
  • 1
  • 1
H.S.
  • 11,654
  • 2
  • 15
  • 32
-1

For main function, it's better to define the second parameter argv as *argv[] or **argv.

This parameter will have no usage if your program doesn't handle any "command line parameters". But if you need it (for example, the -lR string in ls -lR), you may notice that the parameters will become a 2-d char array, or we say an array of (parameter) strings. Since a "string" in C can be char* or char[] in different situations, then we can represent a 2-d char array as char *argv[] or char **argv.

Besides, if you use gcc -Wall, the second program will produce a warning, saying the type of second parameter of main should be char**.

Geno Chen
  • 4,916
  • 6
  • 21
  • 39
  • 1
    *Since a "string" in C can be char* or char[] in different situations, then we can represent a 2-d char array as char *argv[] or char **argv*- A string is always an array of `char`, period. You can point to the first character using `char *` too, as an array would decay in almost all contexts. But `char *argv[]` and `char **argv` - the one that changes is *not* the "string" but the "array". – Antti Haapala -- Слава Україні Dec 08 '18 at 10:55