结构的Malloc

结构的Malloc,c,struct,malloc,C,Struct,Malloc,我在这里没有得到正确的输出, 代码将输入数和选项作为输入,然后将学生年份和性别的名称作为输入,并根据提供的选项给出输出。输出可以是字典中最先出现的名称,也可以是输入中年份的较小值 #include <stdio.h> #include <string.h> #include <stdlib.h> struct student_record { int passing_year; char gender; char name[20];

我在这里没有得到正确的输出, 代码将输入数和选项作为输入,然后将学生年份和性别的名称作为输入,并根据提供的选项给出输出。输出可以是字典中最先出现的名称,也可以是输入中年份的较小值

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student_record
{
    int passing_year;
    char gender;
    char name[20];
};

typedef struct student_record student;

student* find_specific(student* find, int option_num, int number)
{
    int i;
    student* temp = find;
    int opt = option_num;
    int num = number;

    if(opt==1)
    {
        for(i=0; i<num; i++)
        {
            if( strcmp(temp->name, find[i].name) >0)
                temp = find+i;
        }
    }
    else
    {
        for(i=0; i<num; i++)
                {
                    if (temp->passing_year > find[i].passing_year)
                        temp = find+i;
                }
    }

    return temp;

}

int main() {

    student* example;
    student* final;

    int i;
    int option_num, number_of_students;
    printf("Enter 2the number of students, option number");
    scanf("%d" "%d", &number_of_students, &option_num);

    example = (student* )malloc(number_of_students * sizeof(student));

    printf("Enter the name, passing year and gender");
    for(i=0; i< number_of_students; i++)
    {

        scanf("%s" "%d" "%c", example[i].name, &example[i].passing_year, &example[i].gender);
    }

    final = find_specific(example, option_num, number_of_students);

    printf("%s" "%d" "%c", final->name, final->passing_year, final->gender );





    return 0;
}
#包括
#包括
#包括
学生档案结构
{
整年;
性别;
字符名[20];
};
类型定义结构学生\记录学生;
学生*find_特定(学生*find,int选项_num,int编号)
{
int i;
学生*temp=find;
int opt=选项数量;
int num=数字;
如果(opt==1)
{
对于(i=0;在名称中,查找[i].name)>0)
temp=find+i;
}
}
其他的
{
对于(i=0;i通过年>查找[i]。通过年)
temp=find+i;
}
}
返回温度;
}
int main(){
学生*榜样;
学生*期末考试;
int i;
int option_num,学生人数;
printf(“输入2学生人数,选项编号”);
scanf(“%d”%d“、学生人数和选项数量);
示例=(学生*)malloc(学生人数*sizeof(学生));
printf(“输入姓名、过去年份和性别”);
对于(i=0;i<学生人数;i++)
{
scanf(“%s”“%d”“%c”,示例[i]。姓名和示例[i]。过年和示例[i]。性别);
}
最终=查找特定的(例如,选项数量、学生人数);
printf(“%s”“%d”“%c”,final->name,final->passing\u year,final->gender);
返回0;
}

我遇到了一个分割错误。我搞不清楚我到底把事情搞砸了。

你的
scanf()
printf()
格式字符串可能是错误的

scanf("%s" "%d" "%c", example[i].name, &example[i].passing_year, &example[i].gender);
应该是

scanf("%s %d %c", example[i].name, &example[i].passing_year, &example[i].gender);
(没有额外的引号)。编译器将连接相邻的 字符串文本,因此它将您的格式字符串解释为等价于
“%s%d%c”
(中间没有空格),而不是编译器错误。 这可能与输入的布局不匹配,因此某些值可能未初始化,导致以后出现问题

您应该始终检查
scanf
和类似库函数的返回值, 确保您得到了您告诉编译器期望的输入格式。

您的
scanf()
printf()
格式字符串可能是错误的

scanf("%s" "%d" "%c", example[i].name, &example[i].passing_year, &example[i].gender);
应该是

scanf("%s %d %c", example[i].name, &example[i].passing_year, &example[i].gender);
(没有额外的引号)。编译器将连接相邻的 字符串文本,因此它将您的格式字符串解释为等价于
“%s%d%c”
(中间没有空格),而不是编译器错误。 这可能与输入的布局不匹配,因此某些值可能未初始化,导致以后出现问题

您应该始终检查
scanf
和类似库函数的返回值, 确保您得到了您告诉编译器期望的输入格式。

您的
scanf()
printf()
格式字符串可能是错误的

scanf("%s" "%d" "%c", example[i].name, &example[i].passing_year, &example[i].gender);
应该是

scanf("%s %d %c", example[i].name, &example[i].passing_year, &example[i].gender);
(没有额外的引号)。编译器将连接相邻的 字符串文本,因此它将您的格式字符串解释为等价于
“%s%d%c”
(中间没有空格),而不是编译器错误。 这可能与输入的布局不匹配,因此某些值可能未初始化,导致以后出现问题

您应该始终检查
scanf
和类似库函数的返回值, 确保您得到了您告诉编译器期望的输入格式。

您的
scanf()
printf()
格式字符串可能是错误的

scanf("%s" "%d" "%c", example[i].name, &example[i].passing_year, &example[i].gender);
应该是

scanf("%s %d %c", example[i].name, &example[i].passing_year, &example[i].gender);
(没有额外的引号)。编译器将连接相邻的 字符串文本,因此它将您的格式字符串解释为等价于
“%s%d%c”
(中间没有空格),而不是编译器错误。 这可能与输入的布局不匹配,因此某些值可能未初始化,导致以后出现问题

您应该始终检查
scanf
和类似库函数的返回值,
以确保您获得了您告诉编译器期望的输入格式。

“我无法准确地找出我到底在哪里出错。”-当您不确定发生了什么时,总是在调试器下运行,调试器会告诉您。请提供一些示例输入?我喜欢将数字分配给num的方式,只是为了在for循环中使用。。。但是,请检查它是否不是
NULL
“我无法准确地找出我到底在哪里出错。”-当您不确定发生了什么时,总是在调试器下运行,调试器会告诉您。请您提供一些示例输入?我喜欢将数字分配给num的方式,只是为了在for循环中使用。。。但是,请检查它是否不是
NULL
“我无法准确地找出我到底在哪里出错。”-当您不确定发生了什么时,总是在调试器下运行,调试器会告诉您。请您提供一些示例输入?我喜欢将数字分配给num的方式,只是为了在for循环中使用。。。但是,请检查它是否不是
NULL
“我无法准确地找出我到底在哪里出错。”-当您不确定发生了什么时,总是在调试器下运行,调试器会告诉您。请您提供一些示例输入?我喜欢将数字分配给num的方式,只是为了在for循环中使用。。。但是,请检查它是否不是
NULL
。问题是
%c”
说明符与空格匹配,要跳过它,您需要在
%c”
说明符之前有一个显式空格,因此
%s%d%c”
。问题是
%c
说明符与空格匹配,要跳过它,您需要在
%c“
说明符之前有一个显式空格,因此
%s%d%c”<