0

If there is no argument passed from the command line ie. if argc is 1, can we still allocate memory for argv[1],argv[2],..... and use those buffers for further experiments. If that is undefined behavior, can I still use it somehow?

Saurav Rai
  • 2,171
  • 1
  • 15
  • 29

3 Answers3

3

No, the C standard does not specify that argv has any elements beyond argv[argc], so they may not exist in C’s object-memory model and the behavior of using them is not defined by the C standard.

C 2018 5.1.2.2.1 2 says:

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, which are given implementation-defined values by the host environment prior to program startup.

That is all there is that defines the extent of the argv array; nothing in the standard says there are more elements.

When argc is one, using argv[1] is defined but using argv[2] is not.

You can store new values to the defined elements because C 2018 5.1.2.2.1 2 also says:

The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

can I access argv[] elements after argv[argc]?

You can ... but ONLY IN THIS CODE

#include <stdio.h>

int main(int argc, char **argv) {
    if (argc == 1) {
        char *foo[] = {"bar", "baz", "quux", NULL, "bingo"};
        main(3, foo);
    } else {
        printf("argc is %d; argv[4] is \"%s\"\n", argc, argv[4]);
    }
    return 0;
}

See code running on ideone.

In all other codes, you cannot.

pmg
  • 106,608
  • 13
  • 126
  • 198
0

Always argv[argc] is equal to NULL. In the described case where argc is equal to 1 the array argv contains two pointers argv[0] and argv[1] where argv[1] is a null pointer.

You may reassign the pointers but this does not make a great sense because that will make your program unclear. Instead you could declare your own array if you need.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • "You may reassign the pointers" --> [Is `argv[n]` writable?](https://stackoverflow.com/q/25737434/2410359) – chux - Reinstate Monica Sep 26 '22 at 11:49
  • @chux-ReinstateMonica argv is not an array of constant pointers. So the elements of the array may be reassigned. – Vlad from Moscow Sep 26 '22 at 11:58
  • C spec treats `main()` special. Also, lack of `const` does not mean changing is OK. E.g. `char *s = "abc";`. IAC, that old [question](https://stackoverflow.com/q/25737434/2410359) digs into the matter and your insight is appreciated there. – chux - Reinstate Monica Sep 26 '22 at 12:02
  • @chux-ReinstateMonica Elements of the array are pointers. So to write for example argv[1] = "abc"; is a valid code. main is special in the meaning of how the array argv is initialized and nothing more. – Vlad from Moscow Sep 26 '22 at 12:06