【发布时间】:2021-12-08 18:21:21
【问题描述】:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Student {
char name[50];
int age;
} student;
int main() {
char name[50];
int age;
// Requirement values
char stop[] = "stop";
int check = 1;
int countOfStudents = 0;
// Array with students
student* students = malloc(countOfStudents);
while(check) {
scanf("%s", &name);
if(!strcmp(name, stop) == 0) {
scanf(" %i", &age);
struct Student student = {*name, age};
strcpy(students[countOfStudents].name, student.name);
students[countOfStudents].age = student.age;
countOfStudents++;
} else {
printf("You wrote stop \n");
check = 0;
}
}
for (int i = 0; i < countOfStudents; ++i) {
printf("Name = %s , Age = %i", students[i].name, students[i].age);
printf("\n");
}
return 0;
}
我有一个学生数组。每个学生都是一个结构,并且有一个名字和年龄。 我必须注册每个学生,直到用户写“停止”。
输入:
test
12
stop
输出是这样的:
You wrote stop
ogram Files (x86)
Name = t♀ , Age = 0
为什么会这样?
【问题讨论】:
-
嗯,我想知道these errors是什么意思。
-
@SuperStormer 我现在编辑了代码,没有任何警告。我仍然没有得到正确的输出? :O
-
student* students = malloc(countOfStudents);。在此行之前尝试printf("countOfStudents: %d\n", countOfStudents);。 :) 。 + 其他一些人。 -
SuleymanSelcuk,您认为
struct Student student = {{*name, age}};会做什么?注意*name是一个char。
标签: arrays c input struct output