代码结构
代码:
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>//定义结构体
struct Student{char name[10];int score;int age;
};//打印结构体信息
void PrintStruct(const struct Student s)
{printf("Student.name = %s, Student.score = %d, Student.age = %d\n", s.name,s.score, s.age);
}//使用指针操作结构体
void PrintStructByPoint(const struct Student *ps)
{printf("Student.name = %s, Student.score = %d, Student.age = %d\n", ps->name, ps->score, ps->age);
}//结构体数组的操作
void ArrStruct(const int len)
{struct Student* ps = malloc(sizeof(struct Student) * len);for(int i = 0; i < len; i++){//*(ps + i) = {"AA", 88+i, 18+i};strcpy((ps + i)->name, "AA");(ps + i)->score = 88+i;(ps + i)->age = 18+i;}for(int i = 0; i < len; i++){PrintStructByPoint(ps + i);}free(ps);
}int main()
{struct Student s = {"A", 88, 18};//打印结构体信息//PrintStruct(s);//使用指针操作结构体//PrintStructByPoint(&s);//结构体数组的操作int len = 10;ArrStruct(len);return 0;
}
Makefile
main:main.cgcc -o $@ $^./$@
clean:rm main