指针指向堆区空间

指针指向堆区空间
#include <stdio.h>
#include <stdlib.h>

struct Student
{
	int age;
	char name[50];
	int score;
};

int main(int argc, char const *argv[])
{
	//定义一个结构体类型指针
	struct Student *p;

	//指针指向堆区空间
	p = (struct Student *)malloc(sizeof(struct Student));

	if(p == NULL)
	{
		printf("malloc err\n");
		return -1;
	}

	p->age = 18;
	strcpy(p->name, "mike");
	p->score = 59;
	printf("%d, %s, %d\n", p->age, p->name, p->score);

	if(p != NULL)
	{
		free(p);
		p = NULL;
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/CCai_x/article/details/84075016