Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何从其他函数访问结构数组并赋值_C_Arrays_Pointers_Structure - Fatal编程技术网

C 如何从其他函数访问结构数组并赋值

C 如何从其他函数访问结构数组并赋值,c,arrays,pointers,structure,C,Arrays,Pointers,Structure,我的问题是,如果我将全局变量结构数组和整数值计数器从main函数传递到menu2函数,则需要修改结构数组的内容,但没有任何更改。例如,当我在main函数中调用menu2函数并写入内容时,在调用menu2后调用menu1、3和4菜单时不会发生任何事情。我已经尝试将菜单2上的所有“列表”更改为“列表”。你能帮我一次吗 #include<stdio.h> #include<string.h> #define MAX_STUDENT 100 #define YEAR_STUDE

我的问题是,如果我将全局变量结构数组和整数值计数器从main函数传递到menu2函数,则需要修改结构数组的内容,但没有任何更改。例如,当我在main函数中调用menu2函数并写入内容时,在调用menu2后调用menu1、3和4菜单时不会发生任何事情。我已经尝试将菜单2上的所有“列表”更改为“列表”。你能帮我一次吗

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

#define MAX_STUDENT 100
#define YEAR_STUDENT 7
#define MAX_NAMELEN 20


char convertScore(int score)
{
    if (score > 89)
        return 'A';
    else if (score > 79)
        return 'B';
    else if (score > 69)
        return 'C';
    else if (score > 59)
        return 'D';
    else
        return 'F';
}

typedef struct student {
    char name[MAX_STUDENT+1];
    int scoreSecurity;
    char gradeSecurity;
    int scoreAI;
    char gradeAI;
    int scoreAlgorithm;
    char gradeAlgorithm;
    int scoreDataStructure;
    char gradeDataStructure;
    int scoreOS;
    char gradeOS;
    int gradeF;
    char year[YEAR_STUDENT];
}Student;

Student List[MAX_STUDENT];
int Counter = 0;

void menu1(Student* list, int cnt)
{
    int i;
    printf("Name\tAI\tSecurity\tDataStructure\tOS\tAlgorithm\tYear\n");
    for (i = 0; i < cnt; i++) {
        printf("%s\t%c\t%c\t%c\t%c\t%c\t%s\n", List[i].name, List[i].gradeAI,
            List[i].gradeSecurity,List[i].gradeDataStructure, List[i].gradeOS, List[i].gradeAlgorithm,
            List[i].year);
    }
    printf("\n");
}

void menu2(Student* list, int cnt)
{
    char sn[21];
    printf("Student name: ");
    scanf("%s", sn);
    strcpy(List[cnt].name, sn);
    printf("Student score AI(0~100): ");
    scanf("%d", &List[cnt].scoreAI);
    List[cnt].gradeAI = convertScore(List[cnt].scoreAI);
    printf("Student score Security(0~100): ");
    scanf("%d", &List[cnt].scoreSecurity);
    List[cnt].gradeSecurity = convertScore(List[cnt].scoreSecurity);
    printf("Student score Datastructure(0~100): ");
    scanf("%d", &List[cnt].scoreDataStructure);
    List[cnt].gradeDataStructure = convertScore(List[cnt].scoreDataStructure);
    printf("Student score OS(0~100): ");
    scanf("%d", &List[cnt].scoreOS);
    List[cnt].gradeOS = convertScore(List[cnt].scoreOS);
    printf("Student score Algorithm(0~100): ");
    scanf("%d", &List[cnt].scoreAlgorithm);
    List[cnt].gradeAlgorithm = convertScore(List[cnt].scoreAlgorithm);
    printf("Student year(Junior/Senior): ");
    scanf("%s", &List[cnt].year);
    printf("\n");
}

void menu3(Student* list, int cnt)
{
    int junior=0, senior=0, i;
    for (i = 0; i < cnt; i++) {
        if (List[i].year == "Junior")
            junior++;
        else
            senior++;
    }
    printf("Junior: %d\nSenior: %d\n", junior, senior);
}
void menu4(Student* list, int cnt)
{
    int i;
    int fail = 0;
    int suck = 0;
    int yeah = 0;
    for (i = 0; i < cnt; i++) {
        if (List[i].gradeAI == 'F')
            fail++;
        else if (List[i].gradeAlgorithm == 'F')
            fail++;
        else if (List[i].gradeDataStructure == 'F')
            fail++;
        else if (List[i].gradeOS == 'F')
            fail++;
        else if (List[i].gradeSecurity == 'F')
            fail++;

        if (fail > 2)
            suck++;
        else
            yeah++;
        fail = 0;
    }
    printf("Passed: %d\nFailed: %d\n", yeah, suck);
}

#包括
#包括
#定义最大学生100
#定义7年级学生
#定义最大名称20
字符转换分数(整数分数)
{
如果(分数>89)
返回“A”;
否则如果(分数>79)
返回“B”;
否则如果(分数>69)
返回“C”;
否则如果(分数>59)
返回“D”;
其他的
返回“F”;
}
类型定义结构学生{
字符名[MAX_STUDENT+1];
国际安全;
安全性;
int scoreAI;
char gradeAI;
积分算法;
字符分级算法;
数据结构;
字符级数据结构;
int scoreOS;
煤焦;
国际成绩F级;
一年级[一年级学生];
}学生;
学生名单[MAX_Student];
int计数器=0;
无效菜单1(学生*列表,内部cnt)
{
int i;
printf(“Name\tAI\tSecurity\tDataStructure\tOS\tAlgorithm\tYear\n”);
对于(i=0;i2)
吮吸++;
其他的
是的++;
失败=0;
}
printf(“通过:%d\n失败:%d\n”,是的,很糟糕);
}

首先,在
menu2
中,
scanf(“%s”,&List[cnt].year)应为
scanf(“%s”,列表[cnt].year)。第二,我认为你应该考虑你的<代码>计数器>代码>的工作方式。例如,在调用
menu2
,然后调用
menu1
后,无法打印任何内容。原因是在
menu1
中,
for
循环不能执行任何操作。此时,您的
计数器
为0,即
menu1
中的
cnt
为0。

首先,在
menu2
中,
scanf(“%s”,&List[cnt].year)应为
scanf(“%s”,列表[cnt].year)。第二,我认为你应该考虑你的<代码>计数器>代码>的工作方式。例如,在调用
menu2
,然后调用
menu1
后,无法打印任何内容。原因是在
menu1
中,
for
循环不能执行任何操作。此时,您的
计数器
为0,即
menu1
中的
cnt
为0。

谢谢您的建议。我刚刚完美地修改了代码,效果很好:)谢谢你的建议。我刚刚完美地修改了代码,效果很好:)