gpt4 book ai didi

我可以使用 C 指针结构函数更改此 C 代码吗?

转载 作者:行者123 更新时间:2023-11-30 20:33:42 24 4
gpt4 key购买 nike

#define ID_LEN 7
#define NAME_LEN 10

typedef struct {
char id[ID_LEN];
char name[NAME_LEN];
int math;
int eng;
} STUDENT;

void FirstList(STUDENT *list,int i) {
printf("Enter data for student No.%d\n",++i);
printf("ID : ");
scanf("%s",list->id);
printf("Name : ");
scanf("%s",list->name);
printf("Math score: ");
scanf("%d",&list->math);
printf("English score: ");
scanf("%d",&list->eng);
}

int main() {
STUDENT ** list=NULL;
printf("How many student? ");
int num=0;
scanf("%d",&num);
list=(STUDENT**)malloc(num*sizeof(STUDENT*));
for(int i=0; i<num; i++) {
list[i]= malloc(sizeof(STUDENT));
FirstList(list[i],i);
}
}

**这是我的 C 代码的一部分 **

我想更改我的代码而不更改其他部分。我想改变成这样

typedef struct {
char id[ID_LEN];
char name[NAME_LEN];
int math;
int eng;
} STUDENT;

void FirstList(STUDENT *list[] ,int num) {
for(int j=0; j++, j<num) {
list[j]=malloc(sizeof(STUDENT));
printf("Enter data for student No.%d\n",++i);
printf("ID : ");
scanf("%s",list[j]->id);
printf("Name : ");
scanf("%s",list[j]->name);
printf("Math score: ");
scanf("%d",&list[j]->math);
printf("English score: ");
scanf("%d",&list[j]->eng);
}
}

int main() {
STUDENT ** list=NULL;
printf("How many student? ");
int num=0;
scanf("%d",&num);
list=(STUDENT**)malloc(num*sizeof(STUDENT*));
FirstList(list[i],num);
}

我想这样改变。但我不确定这是否可能,并且我试图找到这一点。但我不能......请修复我的代码......请帮助我......感谢您阅读我的问题。

最佳答案

#define ID_LEN 7
#define NAME_LEN 10

typedef struct
{
char id[ID_LEN];
char name[NAME_LEN];
int math;
int eng;
} STUDENT;

void FirstList(STUDENT *list[], int num)
{
int j;
for (j = 0; j < num; j++)
{
list[j] = malloc(sizeof(STUDENT));
printf("Enter data for student No.%d\n", j);
printf("ID : ");
scanf("%s", list[j]->id);
printf("Name : ");
scanf("%s", list[j]->name);
printf("Math score: ");
scanf("%d", &list[j]->math);
printf("English score: ");
scanf("%d", &list[j]->eng);
}
}

void PrintList(STUDENT * list[], int Num)
{
int j;
printf("ID Name Math Eng \r\n");
for (j = 0; j < Num; j++)
{
printf("%4d %8s %4d %4d\r\n", list[j]->id, list[j]->name,
list[j]->math, list[j]->eng);
}
}

int main()
{
STUDENT ** list = NULL;
printf("How many student? ");
int num = 0;
scanf("%d", &num);
list = (STUDENT**)malloc(num * sizeof(STUDENT));
FirstList(list, num);
PrintList(list, num);
return 0;
}

祝你好运!

关于我可以使用 C 指针结构函数更改此 C 代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44451712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com