学了结构体,通过从最简单结构体输入到结构体数组,结构体指针,结构体作为函数参数,包含结构的结构,每种都进行敲代码来理解和体会,另外还学到了栈,先进后出,后进先出,堆需要自己分配,同时一定要有个明确的指针,否则自己都会找到不到,程序中的malloc, calloc, realloc等函数都是分配内存. 由小的空间向大的空间分配, 堆是从下向上分配.至于链表还在练习中
#include <stdio.h>
#include<string.h>
struct Student /*学生结构*/
{
char cName[20]; //姓名
int iNumber; //学好
char cSex; //性别
int iGrade; //年纪
}student;
int main()
{
struct Student*pStruct; //定义结构体指针
pStruct=&student; //指针指向结构体变量
strcpy(pStruct->cName,"SuYuQuan"); //将字符串常亮复制到成员变量中
pStruct->iNumber=12061212; //为成员变量赋值
pStruct->cSex='W';
pStruct->iGrade=2;
printf("----the student's information----\n"); //消息提示
printf("Name:%s\n",student.cName); //使用变量直接输出
printf("Number:%d\n",student.iNumber);
printf("Sex:%c\n",student.cSex);
printf("Grade:%d\n",student.iGrade);
return 0;
}
struct Student{
char cName[20];
int iNumber;
char cSex;
int iGrade;
}student[5]={{"wangjiasheng",12062212,'M',3},
{"yulongjiao",12062213,'W',3},
{"zhangmeng",12062214,'W',3},
{"jiangxuehuan",12062215,'w',3},
{"hanliang",12062216,'M',3}};
int main()
{
struct Student*pStruct;
int index;
pStruct=student;
for (index=0; index<5; index++,pStruct++)
{
printf("NO%d student:\n",index+1);
printf("Name:%s,Number:%d\n",(*pStruct).cName,pStruct->iNumber);
printf("Sex:%c,Grade:%d\n",pStruct->cSex,pStruct->iGrade);
printf("\n");
}
return 0;
}
struct Student
{
char cName[20];
float fScore[3];
}student={"suyuqun",98.5f,89.0,93.5f};
void Display(struct Student* stu)
{
printf("------unformation------\n");
printf("Name:%s\n",stu->cName);
printf("English:%.2f\n",stu->fScore[2]);
stu->fScore[2]=90.0f;
}
int main()
{
struct Student* pStruct=&student;
Display(pStruct);
printf("change english:%.2f\n",pStruct->fScore[2]);
return 0;
}
struct date
{
int year;
int month;
int day;
};
struct student
{
char name[30];
int num;
char sex;
struct date birthday;
}student={"WangWei",12345678.,'M',{1986,12,6}};
int main()
{
printf("-------information---------\n");
printf("Name:%s\n",student.name);
printf("Number:%d\n",student.num);
printf("Sex:%c\n",student.sex);
printf("Brithday:%d,%d,%d\n",student.birthday.year,
student.birthday.month,
student.birthday.day);
return 0;
}