源自:6-12 综合练习
这个代码有什么问题?
#include <stdio.h>
int total(int score[])
{
int sum;
int size = sizeof(score);
for(int i=0; i<size; i++)
{
sum += score[i];
}
return sum;
}
int max(int score[])
{
int max, item;
int size = sizeof(score);
for(int i=0; i<size; i++)
{
item = score[i];
if(item > max)
{
max = item;
}
}
}
int min(int score[])
{
int min, item;
int size = sizeof(score);
for(int i=0; i<size; i++)
{
item = score[i];
if(item < min)
{
min = item;
}
}
}
int average(int score[])
{
return (float)total(score) / sizeof(score);
}
void sort(int score[])
{
int i, temp, flag;
int size = sizeof(score);
while(flag)
{
flag = 0;
for(i=0; i<size-1; i++)
{
if(score[i] > score[i+1])
{
// 交换两元素
temp = score[i];
score[i] = score[i+1];
score[i+1] = temp;
flag = 1;
}
}
}
}
void printScore(int score[])
{
int size = sizeof(score);
for(int i=0; i<size; i++)
{
printf(" %d", score[i]);
}
printf("\n")
}
int main()
{
int score[N]={67,98,75,63,82,79,81,91,66,84};
printf("Test function total(): total(score)=%d\n", total(score));
printf("Test function max(): max(score)=%d\n", max(score));
printf("Test function min(): min(score)=%d\n", min(score));
printf("Test function average(): average(score)=%d\n", average(score));
printf("Test function sort(): sort(score)=");
sort(score);
printScore(score);
return 0;
}
运行失败
hello.c: In function 'total':
hello.c:5:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument]
int size = sizeof(score);
^
hello.c:2:15: note: declared here
int total(int score[])
^~~~~
hello.c: In function 'max':
hello.c:15:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument]
int size = sizeof(score);
^
hello.c:12:13: note: declared here
int max(int score[])
^~~~~
hello.c: In function 'min':
hello.c:28:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument]
int size = sizeof(score);
^
hello.c:25:13: note: declared here
int min(int score[])
^~~~~
hello.c: In function 'average':
hello.c:40:40: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument]
return (float)total(score) / sizeof(score);
^
hello.c:38:17: note: declared here
int average(int score[])
^~~~~
hello.c: In function 'sort':
hello.c:45:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument]
int size = sizeof(score);
^
hello.c:42:15: note: declared here
void sort(int score[])
^~~~~
hello.c: In function 'printScore':
hello.c:64:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument]
int size = sizeof(score);
^
hello.c:62:21: note: declared here
void printScore(int score[])
^~~~~
hello.c:70:1: error: expected ';' before '}' token
}
^
hello.c: In function 'main':
hello.c:73:15: error: 'N' undeclared (first use in this function)
int score[N]={67,98,75,63,82,79,81,91,66,84};
^
hello.c:73:15: note: each undeclared identifier is reported only once for each function it appears in
提问者:慕后端1351523
2020-07-01 20:37