定义一个描述学生基本信息的结构,包括姓名,学号,身份证号,年龄,性别等,并定义一个结构体数组,再定义一个结构体类型的指针,使用结构体指针对数组进行操作,编写函数reverse,将整个数组的元素倒置
时间: 2024-06-06 07:07:30 浏览: 99
#include <stdio.h>
#include <string.h>
struct student {
char name[20];
int num;
char id[18];
int age;
char gender;
};
void reverse(struct student *p, int n);
int main() {
struct student stu[3] = {
{"Tom", 1001, "110101200001011234", 20, 'M'},
{"Mary", 1002, "110101200002022345", 19, 'F'},
{"John", 1003, "110101200003033456", 21, 'M'}
};
struct student *p = stu;
printf("Before reverse:\n");
for (int i = 0; i < 3; i++) {
printf("%s %d %s %d %c\n", (p+i)->name, (p+i)->num, (p+i)->id, (p+i)->age, (p+i)->gender);
}
reverse(p, 3);
printf("After reverse:\n");
for (int i = 0; i < 3; i++) {
printf("%s %d %s %d %c\n", (p+i)->name, (p+i)->num, (p+i)->id, (p+i)->age, (p+i)->gender);
}
return 0;
}
void reverse(struct student *p, int n) {
struct student temp;
for (int i = 0; i < n/2; i++) {
temp = *(p+i);
*(p+i) = *(p+n-1-i);
*(p+n-1-i) = temp;
}
}
阅读全文