#include <stdio.h>
#include <stdlib.h>
struct people {
char name[50];
int age;
float height;
};
int main() {
struct people* ptr; // 指向 struct people 类型的指针
ptr = (struct people*)malloc(sizeof(struct people)); // 动态申请堆区空间
if (ptr == NULL) {
printf("内存分配失败\n");
return 1;
}
printf("请输入姓名: ");
scanf("%s", ptr->name);
printf("请输入年龄: ");
scanf("%d", &(ptr->age));
printf("请输入身高: ");
scanf("%f", &(ptr->height));
printf("\n输入的结构体内容为:\n");
printf("姓名: %s\n", ptr->name);
printf("年龄: %d\n", ptr->age);
printf("身高: %.2f\n", ptr->height);
free(ptr); // 释放动态分配的堆区空间
return 0;
}
在以上代码中,我们定义了一个
struct people
结构体。在main
函数中,首先声明了一个指向struct people
类型的指针