zhangjinghua
2022-01-04 22:22:22 +08:00
#include <stdio.h>
#include <stdlib.h>
int* a = NULL;
int* b()
{
int c[2] = { 0 };
if (c != NULL)
{
c[0] = 123;
c[1] = 456;
}
return c;
}
int main()
{
a = b();
printf("a[0] : %d \n", a[0]);
printf("a[1] : %d \n", a[1]);
printf("Hello World!\n");
}
结果 :
a[0] : 123
a[1] : 0
Hello World!
#include <stdio.h>
#include <stdlib.h>
int* a = NULL;
int* b()
{
static int c[2] = { 0 };
/////////这里加了 static
if (c != NULL)
{
c[0] = 123;
c[1] = 456;
}
return c;
}
int main()
{
a = b();
printf("a[0] : %d \n", a[0]);
printf("a[1] : %d \n", a[1]);
printf("Hello World!\n");
}
结果
a[0] : 123
a[1] : 456
Hello World!
这里为啥会这样呢??? 虽然我知道和变量生命周期有关,但是为什么会留一半呢???