源码:
#include <stdio.h>
const char* p0 = "0";
void show();
void show()
{
const char* p3 = "0";
printf("p3: %p\n", (int*)p3);
}
int main()
{
char s[] = "1";
const char * p1 = "1";
const char* p2 = "2";
printf("s: %p\n" , s);
printf("p0: %p\n" , (int*)p0);
printf("p1: %p\n" , (int*)p1);
printf("p2: %p\n" , (int*)p2);
show();
static int p4 = 1;
printf("i: %p4\n", &p4);
return 0;
}
结果:
s: 0xff92f21a
p0: 0x80485d0
p1: 0x80485da
p2: 0x80485dc
p3: 0x80485d0
i: 0x804a0244
有以下结论:
1.
函数中的const char* p = "1";
"1\0"在.text区域,p在栈
2.
全局变量const char* p = "1";
"1\0"在.text区域,p在.data区域
3.
函数中的char s[] = "1234";
s在栈区域,且被填充为1234\0
4.
全局变量中 char s[] = "123";
s在.data,且被填充为123\0