我正在编写一个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)