为什么在分配用于存储程序中要使用的main参数的指针变量之前未分配空间?

abc*_*019 3 c pointers memory-management argv command-line-arguments

我正在编写一个C程序,要求我接受命令行参数并在程序中使用它们。我不明白的是,为什么在代码中将字符串参数分配给char*变量,以便在程序中进一步使用,而在此之前没有分配内存。在使用指针之前,不必分配足够的内存来指向一个指针吗?

//c program

int main(int argc, char *argv[]){

    //lets say there is only one argument after the program name 
    // so that argc = 2 and argv = {filename, string1}

    //assigning the string to a char *
    //no memory allocated before assignment
    char *x = argv[1];

    // rest of the program ...
}
Run Code Online (Sandbox Code Playgroud)

Rei*_*ica 5

Each item in argv already points to such allocated space, which was created by some C runtime support for storing the command-line arguments. You can therefore safely point to these pre-allocated buffers with your pointers.

Note that this also means you must not try to deallocate them either: whoever created them will be responsible for their correct destruction.