c语言输入结构体字符串数组
时间: 2023-12-18 11:27:22 浏览: 112
以下是一个示例代码,演示如何在C语言中输入结构体字符串数组:
```c
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 50
#define MAX_NUM_STUDENTS 100
struct Student {
char name[MAX_NAME_LEN];
int age;
};
int main() {
struct Student students[MAX_NUM_STUDENTS];
int num_students;
printf("Enter the number of students: ");
scanf("%d", &num_students);
for (int i = 0; i < num_students; i++) {
printf("Enter the name of student %d: ", i+1);
scanf("%s", students[i].name);
printf("Enter the age of student %d: ", i+1);
scanf("%d", &students[i].age);
}
printf("The list of students is:\n");
for (int i = 0; i < num_students; i++) {
printf("Student %d: %s, %d years old\n", i+1, students[i].name, students[i].age);
}
return 0;
}
```
该代码定义了一个名为`Student`的结构体,其中包含一个名为`name`的字符串数组和一个名为`age`的整型变量。然后,它使用`scanf`函数从用户输入中读取学生的姓名和年龄,并将它们存储在结构体数组`students`中。最后,它遍历结构体数组并打印每个学生的姓名和年龄。
阅读全文