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.htmlhtml

相關文章
相關標籤/搜索