【发布时间】:2017-04-17 12:27:49
【问题描述】:
#include <stdio.h>
#include <cs50.h>
int main (void)
{
int *x;
x = malloc(sizeof(long long)*3);
scanf("%i %i %i",x, (x+1), (x+2));
printf("%i\t %i\t %i\n",(int)x, (int)(x+1), (int)(x+2));
printf("%i\t %i\t %i\n",*x, *(x+1), *(x+2));
free(x);
}
这个程序对于输入 12,2,3 的输出是:
43171856 43171860 43171864
12 2 3
所以,我的问题是为什么地址之间的差异在每种情况下都是 4,
如果*x 指向43171856 那么*(x+1) 应该指向4317185 而不是43171860? sizeof(long long) 也是 8 字节,所以分配的内存如何在 4 字节之间分配 8 字节在 43171856 和 43171860 之间。
【问题讨论】:
标签: c pointers malloc dynamic-memory-allocation cs50