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?

- 2,171
- 1
- 15
- 29
-
If `argc` is `1` then accessing `argv[1]` or higher is probably UB. – Joachim Sauer Sep 26 '22 at 10:15
-
@JoachimSauer If that leads to UB, can I somehow use it? workaround? – Saurav Rai Sep 26 '22 at 10:18
-
6@JoachimSauer If `argc` is `1` then `argv[1]` will be accessible, but it will contain a null pointer. – Ian Abbott Sep 26 '22 at 10:19
-
@IanAbbott what about {argv[2], argv[3],...} Is it the same case? can we use it? – Saurav Rai Sep 26 '22 at 10:20
-
`argv[argc] == NULL` (not 100% sure that is C standard, it might also be just POSIX or implementation specific). Accessing `argv[argc+1]` or beyond is definitely Undefined Behavior. You may modify pointers in valid range of `argv`, and it is actually somewhat common practice (to hide command line arguments which are already handled, keeping unhandled ones available for other code). – hyde Sep 26 '22 at 10:20
-
@IanAbbott: fair, my C days are long ago. I think what I said still applies to `argv[2]` and up though. – Joachim Sauer Sep 26 '22 at 10:21
-
Can you drive a car that doesn't exist? – Andrew Henle Sep 26 '22 at 10:22
-
2@hyde `argv[argc]` is required by the C Standard to be `NULL`. [cppreference.com](https://en.cppreference.com/w/c/language/main_function) – Dúthomhas Sep 26 '22 at 10:27
-
2It is more like if your cars is announced to have 2 seats, can you use seat #3 and up? No, you cannot, and you cannot "make" more seats, as the car is immutable. – the busybee Sep 26 '22 at 10:27
-
@thebusybee Fair enough :) – Saurav Rai Sep 26 '22 at 10:28
-
It is also possible for `argc` to be `0`, in which case `argv[0]` will be `NULL`. – Ian Abbott Sep 26 '22 at 10:29
-
See also [Is it possible to add an argument to main by scanf?](https://stackoverflow.com/q/30904917/2410359). – chux - Reinstate Monica Sep 26 '22 at 11:50
-
One way is to re-assign `argv` to a new array of `char *`. – chux - Reinstate Monica Sep 26 '22 at 11:55
3 Answers
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 membersargv[0]
throughargv[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
andargv
and the strings pointed to by theargv
array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

- 195,579
- 13
- 168
- 312
can I access
argv[]
elements afterargv[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;
}
In all other codes, you cannot.

- 106,608
- 13
- 126
- 198
-
-
That only works if the original `argc` satisfies `argc == 1 || argc > 4`. – Ian Abbott Sep 26 '22 at 10:37
-
-
@pmg So if I have to use dynamic memory allocation instead of this static allocation. I need to free the character buffers...hence this won't work – Saurav Rai Sep 26 '22 at 10:50
-
1
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.

- 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