今天覆習數據結構知識的時候,看到結構體的一個有趣特性,下面附上代碼:
#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的地址。我能想到的就是以上,如果有朋友看到有其他想補充的歡迎交流呀,萌新一定虛心學習。