【问题标题】:Can't get the name and age of each student. Instead I get weird characters and age = 0?无法获得每个学生的姓名和年龄。相反,我得到了奇怪的字符和年龄 = 0?
【发布时间】: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


【解决方案1】:

至少这个问题

分配错误

int countOfStudents = 0;
student* students = malloc(countOfStudents);

分配零内存。

试试

int countOfStudents = 0;
int Students_MAX = 100;
student* students = malloc(sizeof *students * Students_MAX);

并限制迭代:

while(countOfStudents < Students_MAX && check)

最好使用宽度限制

char name[50];
...
// scanf("%s", name);
scanf("%49s", name);

struct Student student = {{*name, age}}; 不是必需的初始化。

【讨论】:

    【解决方案2】:

    内存分配错误。 以下代码将起作用。

    #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;
    
        char stop[] = "stop";
        int check = 1;
    
        int countOfStudents = 0;
    
        student* students = NULL;
    
        while(check)
        {
            scanf("%49s", name); // 49 + '\0' = 50
            if(!strcmp(name, stop) == 0)
            {
                scanf(" %i", &age);
                if(!students)
                {
                    students = (student*) malloc(sizeof(student*)); // allocating for the first
                }
                else
                {
                    students = (student*) realloc(students, sizeof(student*)); // allocating 
                }
                strcpy(students[countOfStudents].name, name);
                students[countOfStudents].age = 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");
        }
    
        free(students);
        students = NULL;
    
        return 0;
    }
    

    【讨论】:

    • sizeof(student*)指针的大小。建议sizeof *students.
    • realloc(students, sizeof(student*)) --> 需要按countOfStudents+1扩展。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    相关资源
    最近更新 更多