【发布时间】:2017-06-09 07:21:27
【问题描述】:
#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);
}
我想这样改变。但我不确定这是否可能,我试图找到这个。但我不能....请修复我的代码.... 请帮助我....感谢您阅读我的问题。
【问题讨论】:
-
void FirstList(STUDENT *list[] ,int num)你不能给数组。您只能将指针作为函数中的参数。另外,你应该养成检查malloc的返回值的习惯。 -
for(int j=0; j++, j<num)==>for(int j=0; j<num; j++) -
请展示您迄今为止的研究/调试工作。请先阅读How to Ask页面。
-
@SouravGhosh 你能告诉我我的想法是否可行吗?
-
@Codingdumb 不是。
标签: c function pointers struct