C语言问题 使用字符数组和实型数组分别存储学生姓名和成绩,并通过对学生成绩的排序,按名次输出姓名成绩
我只学到数组和基础的内容,未学结构体。程序已经写好,也通过编译,但对于编译器的Warning信息,我不知如何修改,望指点。我主要用冒泡排序,并在对成绩排序的同时也对名字排...
我只学到数组和基础的内容,未学结构体。
程序已经写好,也通过编译,但对于编译器的Warning信息,我不知如何修改,望指点。
我主要用冒泡排序,并在对成绩排序的同时也对名字排序。
#include<stdio.h>
#include<string.h>
int main()
{
char* Name[100][20],cTemp[20];
float mark[100],fTemp;
int i,j,iNum;
puts("请输入学生数");
scanf("%d",&iNum);
puts("请分别输入姓名和成绩");
for(i=0;i<iNum;i++)
{
scanf("%s",&Name[i]);
scanf("%f",&mark[i]);
}
for(i=1;i<iNum;i++) //排序开始(冒泡)
{
for(j=iNum-1;j>=i;j--)
{
if(mark[j]>mark[j-1])
{
fTemp=mark[j-1]; //交换成绩
mark[j-1]=mark[j];
mark[j]=fTemp;
strcpy(cTemp,Name[j-1]); //交换姓名
strcpy(Name[j-1],Name[j]);
strcpy(Name[j],cTemp);
}
}
}
printf("名次\t姓名\t成绩:\n");
for(i=0;i<iNum;i++)
{
printf("%d\t%s\t%f\n",i+1,Name[i],mark[i]);
}
return 0;
}
这是编译器的提示:
In function 'main':
(行 列)
28 5 [Warning] passing argument 2 of 'strcpy' from incompatible pointer type [enabled by default]
2 0 In file included from G:\范例\8.9.2.c
51 18 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\string.h [Note] expected 'const char * __restrict__' but argument is of type 'char **'
29 5 [Warning] passing argument 1 of 'strcpy' from incompatible pointer type [enabled by default]
2 0 In file included from G:\范例\8.9.2.c
51 18 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\string.h [Note] expected 'char * __restrict__' but argument is of type 'char **'
29 5 [Warning] passing argument 2 of 'strcpy' from incompatible pointer type [enabled by default]
2 0 In file included from G:\范例\8.9.2.c
51 18 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\string.h [Note] expected 'const char * __restrict__' but argument is of type 'char **'
30 5 [Warning] passing argument 1 of 'strcpy' from incompatible pointer type [enabled by default]
2 0 In file included from G:\范例\8.9.2.c
51 18 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\string.h [Note] expected 'char * __restrict__' but argument is of type 'char **' 展开
程序已经写好,也通过编译,但对于编译器的Warning信息,我不知如何修改,望指点。
我主要用冒泡排序,并在对成绩排序的同时也对名字排序。
#include<stdio.h>
#include<string.h>
int main()
{
char* Name[100][20],cTemp[20];
float mark[100],fTemp;
int i,j,iNum;
puts("请输入学生数");
scanf("%d",&iNum);
puts("请分别输入姓名和成绩");
for(i=0;i<iNum;i++)
{
scanf("%s",&Name[i]);
scanf("%f",&mark[i]);
}
for(i=1;i<iNum;i++) //排序开始(冒泡)
{
for(j=iNum-1;j>=i;j--)
{
if(mark[j]>mark[j-1])
{
fTemp=mark[j-1]; //交换成绩
mark[j-1]=mark[j];
mark[j]=fTemp;
strcpy(cTemp,Name[j-1]); //交换姓名
strcpy(Name[j-1],Name[j]);
strcpy(Name[j],cTemp);
}
}
}
printf("名次\t姓名\t成绩:\n");
for(i=0;i<iNum;i++)
{
printf("%d\t%s\t%f\n",i+1,Name[i],mark[i]);
}
return 0;
}
这是编译器的提示:
In function 'main':
(行 列)
28 5 [Warning] passing argument 2 of 'strcpy' from incompatible pointer type [enabled by default]
2 0 In file included from G:\范例\8.9.2.c
51 18 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\string.h [Note] expected 'const char * __restrict__' but argument is of type 'char **'
29 5 [Warning] passing argument 1 of 'strcpy' from incompatible pointer type [enabled by default]
2 0 In file included from G:\范例\8.9.2.c
51 18 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\string.h [Note] expected 'char * __restrict__' but argument is of type 'char **'
29 5 [Warning] passing argument 2 of 'strcpy' from incompatible pointer type [enabled by default]
2 0 In file included from G:\范例\8.9.2.c
51 18 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\string.h [Note] expected 'const char * __restrict__' but argument is of type 'char **'
30 5 [Warning] passing argument 1 of 'strcpy' from incompatible pointer type [enabled by default]
2 0 In file included from G:\范例\8.9.2.c
51 18 c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\include\string.h [Note] expected 'char * __restrict__' but argument is of type 'char **' 展开
4个回答
展开全部
两处明显错误,一个是字符数组用错了,char name[30]这个只能用来存储一个学生姓名,并不是你想的30个,第二个是冒泡法写反了,再仔细看看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看上去,排序代码应该是正确的,唯有使用charstemp[1]做临时字符串变量,感觉怪怪的,建议换成非数组的charstemp字串试试,即
char* charstemp;
charstemp=chars[j];
chars[k+1]=charstemp;
char* charstemp;
charstemp=chars[j];
chars[k+1]=charstemp;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
char* Name[100][20],cTemp[20];
多了个*,删除后运行通过
多了个*,删除后运行通过
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询