今天复习数据结构知识的时候,看到结构体的一个有趣特性,下面附上代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct test
{
int arr[3];
int last;
int end;
}test;
int main(int args,char **argv)
{
test *sq;
sq = malloc(sizeof(sq));
sq -> arr[0] = 666;
sq -> arr[1] = 222;
sq -> arr[2] = 333;
sq -> arr[3] = 444;
sq -> arr[4] = 555;
sq-> last = 8;
sq-> end = 9;
printf("0: %d\n",sq->arr[0] );
printf("3: %d\n",sq->arr[3] );
printf("4: %d\n",sq->arr[4] );
printf("last: %d\n",sq->last );
printf("end: %d\n",sq->end );
}
输出结果为:
分析了一下,其实last的值是arr[3]的值,end的值是arr[4]的值,也就是说malloc开辟的是一片连续的空间,arr[3],arr[4]的地址就是last和end的地址。我能想到的就是以上,如果有朋友看到有其他想补充的欢迎交流呀,萌新一定虚心学习。