【问题标题】:Assigning user input values to allocated memory将用户输入值分配给分配的内存
【发布时间】:2015-01-11 10:20:31
【问题描述】:

该程序背后的想法是为学生信息声明一个数据结构,然后让用户在数据结构的每个字段中为输入的学生数量输入信息。我的问题是当我输入名字时程序停止工作。怎么了?感谢您的宝贵时间。

//Inclusion of necessary header files
#include <stdio.h>
#include <stdlib.h>

//Data structure declaration
struct student{
char firstName[20];
char lastName[20];
char id[10];
char gender;
int age;
double gpa;
};

//Function prototypes
void readStudentsInformation(struct student *, int size); 
void outputStudents(struct student *, int size);
double averageGPA(struct student *, int size);
void sortByLastName(struct student *, int size);
void sortByGPA(struct student *, int size);


//Entry point
int main()
{
    //Variable delcaration
    int size;
    struct student *ptr;
    //Input prompt and function
    printf("How many students?\n");
    scanf_s("%d", &size);

    //Allocation memory for struct student times the number of students and assigning it to a struct student pointer for external function modification
    ptr = (struct student*)malloc(sizeof(struct student)*size);

    //readStudentsInformation Function call
    readStudentsInformation(ptr, size);

    //Exit sequence
    return 0;
}

//This functions reads the information for all the students from the keyboard, taking the class size through the pointer method and struct student from main
void readStudentsInformation(struct student *ptr, int size) 
{
    //For loop controller declaration
    int i;
    //Function message
    printf("Student Information Form\n");
    //This for loop increments the "index" of each students info location where user input is stored
    for(i=0;i<size;i++)
    {
        //Each field has it's appropriate input limit
        printf("Please enter Student %d's First Name(20 characters).\n", i+1);
        scanf_s("%20s", ptr[i].firstName);
        printf("Please enter Student %d's Last Name(20 characters).\n", i+1);       
        scanf_s("%20s", ptr[i].lastName);                                   
        printf("Please enter Student %d's ID(10 characters).\n",i+1);               
        scanf_s("%10s", ptr[i].id);
        printf("Please enter Student %d's gender(M/F).\n",i+1);
        scanf_s("%c", ptr[i].gender);
        printf("Please enter Student %d's age.\n",i+1);
        scanf_s("%3d", ptr[i].age);   //Only 3 digits can be put in at a time
        printf("Please enter Student %d's GPA.\n", i+1);
        scanf_s("%.1lf", ptr[i].gpa);   //From the lab it can be seen that no more than one decimal place is featured, so the same is done here
    }

    //Exit to main
    return;
}

【问题讨论】:

  • 我已经调整了scanf_s,但是程序似乎仍然崩溃。我觉得这不是正确的地址。如果我声明一个 struct student 类型的数组并根据用户输入确定其数组大小怎么办?

标签: c pointers data-structures malloc variable-assignment


【解决方案1】:

你应该这样做

scanf_s("%20s", ptr[i].firstName);

读名字。 ptr+i 会给你student 结构。您想将数据存储在结构成员中。

为此也更改您的其他scanfs。

【讨论】:

  • 否,缺少尺寸参数。 "fscanf_s 函数等价于 fscanf,不同之处在于 c、s 和 [ 转换说明符适用于一对参数......"
【解决方案2】:

你可以使用

scanf("%s",ptr[i].firstName);

对于你的 scanf() 中的其余结构成员,依此类推。

【讨论】:

    【解决方案3】:

    scanf_s 更改为 scanf 并将此块更改如下:

        scanf("%c", &ptr[i].gender);
        printf("Please enter Student %d's age.\n",i+1);
        scanf("%3d", &ptr[i].age);   //Only 3 digits can be put in at a time
        printf("Please enter Student %d's GPA.\n", i+1);
        scanf("%1lf", &ptr[i].gpa);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 2014-12-20
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      • 2016-07-31
      相关资源
      最近更新 更多