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

时间:2021-04-27 19:18:14
#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);
    }
}

**It is part of my C code **

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

I wanna change of my code without changing the other part. I wanna change Like this

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

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);
}

I wanna change like this. But I'm not sure it is possible, and I tried to find about this. But I couldn't....Please fix my code.... Please help me.... Thank you for reading my question.

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

1 个解决方案

#1


0  

#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;
}

Good Luck!

#1


0  

#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;
}

Good Luck!