0

When writing the C main function, it takes in argc and argv. Why is argc needed? Can't you just figure out the size of argv by sizeof(argv)/sizeof(char *);

JobHunter69
  • 1,706
  • 5
  • 25
  • 49
  • 4
    No. `argv` is only a pointer, too. In fact, `main()` is an ordinary function. No function can receive anything more that the address of the first element of an array. `argc` is a convenience to know how many valid pointers one will find in the array of pointers named `argv`... (Little known fact is that `argv[ argc ]` will always be set to NULL. Yes, there is a marker at the end of the array, but it would be tedious for every program to perform something analogous to `strlen()`...) – Fe2O3 Feb 02 '23 at 05:51
  • 1
    PS: **IF** it were possible to use `sizeof` (it isn't), `sizeof argv/sizeof argv[0]` would be a better way to write it... Think in terms of the objects not their types. – Fe2O3 Feb 02 '23 at 06:16

0 Answers0