I don’t understand how it works under the hood. How can C determine how much memory to allocate for argv when the program doesn’t know the number of arguments until it is running?
I tried understanding how C can figure out how much memory to allocate for argv[], since the program doesn’t know how many arguments will be passed until it's actually running. I asked GPT but i didn't really like its answer
How can
argv
in C be an array if its size is determined dynamically?
Parameter char *argv[]
is not an array even though it has a []
. argv
is a pointer to the first element of an array of pointers. The first array elements point to strings. The last one is equal to NULL
.
How can C determine how much memory to allocate for
argv
when the program doesn’t know the number of arguments until it is running?
The memory is allocated before the program is called. The calling code (e.g. the operating system's command line processor) has determined the memory allocation. How it does that is not specified by C.
I asked GPT but i didn't really like its answer