为什么我的MacBook Pro 2015型号核心i5的内存有时为4.5字节,有时为6字节?
我在C中运行了此代码。
#include <stdio.h>
#include <string.h>
int main(){
char ch[] = "Hello World!";
char *p1 = "Hello World!";
printf("%p %s\n", &ch , ch);
printf("size of = %lu bytes\n", sizeof(&ch));
printf("%p %s\n", p1, p1);
printf("size of = %lu bytes\n", sizeof(p1));
return 0;
}
我的终端输出是
0x7ffee8f54a2b Hello World!
size of = 8 bytes
0x106cabf88 Hello World!
size of = 8 bytes
为什么我在第一种情况下得到一个6字节的内存地址,而在第二种情况下得到4.5个内存地址?
在此声明中
printf("%p %s\n", &ch , ch);
输出了变量ch的地址,并声明了自动存储持续时间,例如
char ch[] = "Hello World!";
在此声明中
printf("%p %s\n", p1, p1);
输出在指针p1
中存储的值,该值是字符串文字“ Hello World!”的第一个字符的地址。存储在静态内存中的内存(具有静态存储持续时间的内存)。
因此您正在输出不同种类的内存的地址。