C语言 undeclared identifier问题
写了一个程序,运行结果是undeclaredidentifier,请大神指教!#include<stdio.h>#include<malloc.h>voidInput(s...
写了一个程序,运行结果是undeclared identifier,请大神指教!
# include <stdio.h>
# include <malloc.h>
void Input(struct Student *);
void Output(struct Student *);
void Sort(struct Student *);
struct Student
{
char name[100];
int age;
float score;
};
int main(void)
{
struct Student * pArr;
int len = (int)malloc(sizeof(int));
printf("请输入学生个数:");
scanf("%d\n", &len);
pArr = (struct Student * )malloc(len * sizeof(struct Student));
Input(pArr);
Sort(pArr);
Output(pArr);
return 0;
}
void Input(struct Student * pArr)
{
int i = 0;
for(i=0; i<len; ++i)
{
printf("请输入第%d个学生的信息:\n", i+1);
printf("name = ");
scanf("%s\n", pArr[i].name);
printf("age = ");
scanf("%d\n", &pArr[i].age);
printf("score = ");
scanf("score = ", &pArr[i].score);
}
}
void Sort(struct Student * pArr)
{
struct Student t;
int i, j;
for(i=0; i<len-1; ++i)
{
for(j=0; j<len-1-i; ++j)
{
if(pArr[j].score > pArr[j+1].score)
t = pArr[j];
pArr[j] = pArr[j+1];
pArr[j+1] = t;
}
}
}
void Output(struct Student * pArr)
{
int i = 0;
for(i=0; i<len; ++i)
{
printf("第%d个学生的信息是:", i+1);
printf("name = %s\n", pArr[i].name);
printf("age = %d\n", pArr[i].age);
printf("score = %f\n", pArr[i].score);
}
}
错误在Input函数for循环里的len那里,说是undeclared identifier,可是我之前是用malloc定义了的啊。 展开
# include <stdio.h>
# include <malloc.h>
void Input(struct Student *);
void Output(struct Student *);
void Sort(struct Student *);
struct Student
{
char name[100];
int age;
float score;
};
int main(void)
{
struct Student * pArr;
int len = (int)malloc(sizeof(int));
printf("请输入学生个数:");
scanf("%d\n", &len);
pArr = (struct Student * )malloc(len * sizeof(struct Student));
Input(pArr);
Sort(pArr);
Output(pArr);
return 0;
}
void Input(struct Student * pArr)
{
int i = 0;
for(i=0; i<len; ++i)
{
printf("请输入第%d个学生的信息:\n", i+1);
printf("name = ");
scanf("%s\n", pArr[i].name);
printf("age = ");
scanf("%d\n", &pArr[i].age);
printf("score = ");
scanf("score = ", &pArr[i].score);
}
}
void Sort(struct Student * pArr)
{
struct Student t;
int i, j;
for(i=0; i<len-1; ++i)
{
for(j=0; j<len-1-i; ++j)
{
if(pArr[j].score > pArr[j+1].score)
t = pArr[j];
pArr[j] = pArr[j+1];
pArr[j+1] = t;
}
}
}
void Output(struct Student * pArr)
{
int i = 0;
for(i=0; i<len; ++i)
{
printf("第%d个学生的信息是:", i+1);
printf("name = %s\n", pArr[i].name);
printf("age = %d\n", pArr[i].age);
printf("score = %f\n", pArr[i].score);
}
}
错误在Input函数for循环里的len那里,说是undeclared identifier,可是我之前是用malloc定义了的啊。 展开
展开全部
修改后的代码:
# include <stdio.h>
# include <malloc.h>
struct Student
{
char name[100];
int age;
float score;
};
//改一下位置,并添加参数
void Input(struct Student *, int);
void Output(struct Student *, int);
void Sort(struct Student *, int);
int main(void)
{
struct Student * pArr;
int len = (int)malloc(sizeof(int));
printf("请输入学生个数:");
scanf("%d\n", &len);
pArr = (struct Student * )malloc(len * sizeof(struct Student));
//改变调用方式
Input(pArr, len);
Sort(pArr, len);
Output(pArr, len);
return 0;
}
void Input(struct Student * pArr, int len) //加一个参数
{
int i = 0;
for(i=0; i<len; ++i)
{
printf("请输入第%d个学生的信息:\n", i+1);
printf("name = ");
scanf("%s\n", pArr[i].name);
printf("age = ");
scanf("%d\n", &pArr[i].age);
printf("score = ");
scanf("score = ", &pArr[i].score);
}
}
void Sort(struct Student * pArr, int len) //加一个参数
{
struct Student t;
int i, j;
for(i=0; i<len-1; ++i)
{
for(j=0; j<len-1-i; ++j)
{
if(pArr[j].score > pArr[j+1].score)
t = pArr[j];
pArr[j] = pArr[j+1];
pArr[j+1] = t;
}
}
}
void Output(struct Student * pArr, int len) //加个参数
{
int i = 0;
for(i=0; i<len; ++i)
{
printf("第%d个学生的信息是:", i+1);
printf("name = %s\n", pArr[i].name);
printf("age = %d\n", pArr[i].age);
printf("score = %f\n", pArr[i].score);
}
}
追问
谢谢,已经无错了,可是运行起来,只有一个输入学生数量,后面就没了,怎么没有调用函数呢?
顺便问一下,你用的这个软件是什么名字?命令可以变颜色的。
追答
你这里没有实现动态生成链表中的节点,你的节点生成应该放在Input函数里面,这样根据循环,每次循环生成一个新节点来存储新的信息。
你说的软件是指我上面发的代码吗?那个不是我用的编辑器,这个是百度知道自带的代码格式化工具,答题者一般都可以使用的,可以是代码格式化变得更容易阅读。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询