目录
前言
开启结构体学习之路!
一、结构体定义和使用
1.1结构体定义
struct student //结构体定义
{
char *name;
int scores;
};//特别提醒!!!别忘记;
1.2结构体声明
二、结构体使用
1.代码示例
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
struct student //结构体定义
{
char *name;
int scores;
};
int main()
{
//类型 变量名 初始值
int a = 10;
//struct student stu1 = {100,"张三"};
struct student stu1;
//stu.name = "张三"; 错误格式!!!!
stu1.name = (char *)malloc(128);
memset(stu1.name,'\0',128);
strcpy(stu1.name,"张三");
stu1.scores = 100;
printf("学生姓名:%s\n",stu1.name);
printf("学生成绩为:%d\n",stu1.scores);
return 0;
}
总结
注意在结构体里面定义指针变量的时候,一定要注意段错误,注意给指针变量开辟内存空间。