关于结构体内存的一个特性

今天复习数据结构知识的时候,看到结构体的一个有趣特性,下面附上代码:

#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的地址。我能想到的就是以上,如果有朋友看到有其他想补充的欢迎交流呀,萌新一定虚心学习。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值