C 语言val

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>


typedef struct temp{
	int len;
	int data[0];
};

typedef struct temp*  sp;


typedef struct _student
{
	     int id;
	     int age;
}student_st;

typedef struct _student* student;


int main(int argc, char** argv)
{
	
	sp _sp = NULL;

	student stu = (student_st *)malloc(sizeof(student_st));
	stu->id = 100;
	stu->age = 23;

	student tmp = NULL;


	_sp = (struct temp*)malloc(sizeof(struct temp) + sizeof(student_st));
	if (_sp == NULL)
	{
		printf("malloc memory failed\r\n");
		return -1;
	}
	memset(_sp, 0, sizeof(struct temp) + sizeof(student_st));

	_sp->len = sizeof(student_st);
	memcpy(_sp->data,stu,_sp->len);
	
	printf("buff1 address:%p,buff1->data_len address:%p,buff1->data address:%p\n", _sp, &(_sp->len), _sp->data);
	
	
	tmp = (student_st*)(_sp->data);
	
	printf("_sp->data addr %p,id addr is:%p,id is:%d,age addr is:%p, id is:%d\r\n", _sp->data, &tmp->id, tmp->id, &tmp->age, tmp->age);

	free(_sp);


	_sp = (struct temp*)malloc(sizeof(struct temp) + 10 * sizeof(int));

	memset(_sp, 0, sizeof(struct temp) + 10 * sizeof(int));

	_sp->len = 10;
	
	for (int i = 0; i < 10; i++)
	{
		_sp->data[i] = i;
	}


	for (int i = 0; i < 10; i++)
	{
		printf("%d\r\n", _sp->data[i]);
	}

	free(_sp);


	return 0;
}

参考链接 : https://www.cnblogs.com/Anker/p/3744127.html

转载于:https://my.oschina.net/u/1579560/blog/3007830

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值