-8
int main( int argc, char **argv ) {

 }

In this specific program, I want to check the whether the length of the char **argv is match to the argument count. However, I am not sure how I can loop through the character array of pointer of pointer.

Can anyone give me a suggestion? I am very new to C from Java base.

  • 1
    You might get a response if you explain _**why**_ you want to do this... – Fe2O3 Sep 30 '22 at 07:06
  • 5
    The value in `argc` **is** the length of the `argv` array. That's the contract, you can rely on. What do really want to do? – harper Sep 30 '22 at 07:07
  • 1
    https://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean – harper Sep 30 '22 at 07:09
  • I think this question has answers for you: https://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean – mmixLinus Sep 30 '22 at 07:09
  • 1
    @harper Actually, it's my experience that the array has a NULL pointer in the last element, so argc is actually 1 less that the number of elements... This is my experience. It may not be gospel in all implementations. – Fe2O3 Sep 30 '22 at 07:09
  • 3
    @Fe2O3 this is required by standard since, at least, C99. – Yakov Galka Sep 30 '22 at 07:11
  • @YakovGalka It's been the same since C90. – Lundin Sep 30 '22 at 08:40

1 Answers1

2

The C standard is pretty self-explanatory in this case. Relevant parts from C17 5.1.2.2.1:

  • argv[argc] shall be a null pointer.
  • If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, ...
  • If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

You don't need to check if your compiler fulfils the above requirements unless you have reason to believe it is non-conforming, in which case all bets of program behavior are off anyway.

Lundin
  • 195,001
  • 40
  • 254
  • 396