为什么第一个函数返回字符串"Hello, World"而第二个函数没有返回任何内容。我认为两个函数的返回值都应该是未定义的,因为它们返回超出作用域的数据。
为什么第一个函数返回字符串"Hello, World"而第二个函数没有返回任何内容。我认为两个函数的返回值都应该是未定义的,因为它们返回超出作用域的数据。
为什么第一个函数返回字符串"Hello, World"而第二个函数没有返回任何内容。我认为两个函数的返回值都应该是未定义的,因为它们返回超出作用域的数据。
#include <stdio.h>
// This successfully returns "Hello, World"
char* function1()
{
char* string = "Hello, World!";
return string;
}
// This returns nothing
char* function2()
{
char string[] = "Hello, World!";
return string;
}
int main()
{
char* foo1 = function1();
printf("%s\n", foo1); // Prints "Hello, World"
printf("------------\n");
char* foo2 = function2(); // Prints nothing
printf("%s\n", foo2);
return 0;
}
char[]
和char *
之间的区别。 - machine_1