如何将数组传递到C中的函数(在Fedora上)?
因为我是初学者,所以一直在练习将数组作为参数传递给函数。有人能解释一下为什么我写的这个程序在codeblox上运行得很好,但在Fedora上的Fedora终端上不起作用吗?它对任何分数都不起作用。我正在使用GCC编译器如何将数组传递到C中的函数(在Fedora上)?,c,arrays,function,fedora,stdio,C,Arrays,Function,Fedora,Stdio,因为我是初学者,所以一直在练习将数组作为参数传递给函数。有人能解释一下为什么我写的这个程序在codeblox上运行得很好,但在Fedora上的Fedora终端上不起作用吗?它对任何分数都不起作用。我正在使用GCC编译器 #include<stdio.h> int num,scores[]={},count,max,sum; void input(int scores[]); int findmax(int scores[]); int findsum(int scores[]); v
#include<stdio.h>
int num,scores[]={},count,max,sum;
void input(int scores[]);
int findmax(int scores[]);
int findsum(int scores[]);
void display(int max,int sum);
int main()
{
printf("Enter number of scores\n");
scanf("%d",&num);
input(scores);
max=findmax(scores);
sum=findsum(scores);
display(max,sum);
return 0;
}
void input(int scores[])
{
for(count=0; count<num; count ++)
{
printf("Enter score #%d:\n",count);
scanf("%d",&scores[count]);
}
}
int findmax(int scores[])
{
for(count=0; count<num ; count ++)
{
if(scores[count]>max)
{
max=scores[count];
}
}
return max;
}
int findsum(int scores[])
{ sum=0;
for(count=0; count<num ; count++ )
{
sum=sum+scores[count];
}
return sum;
}
void display(int max,int sum)
{
printf("The max score is :%d\nThe total score is:%d\n",max,sum);
}
除非绝对需要,否则应避免使用全局变量。这里不需要。所有值都应声明为main的本地值,并应根据需要作为参数传递给每个函数
您必须至少通过检查scanf的返回或正在使用的任何输入函数来验证所有输入。如果用户只需按Enter键生成“\n”或输入单词而不是数字,导致转换失败,那么您就死在水中,很可能会看到一个无限循环滚动而过,直到您终止该过程。猫可能会踩键盘等
使用scanf系列函数时,如果出现故障,则必须说明输入缓冲区中剩余的任何字符,例如带scanf的stdin。如果在循环中调用scanf,则可能会出现无限循环,其中转换失败,输入缓冲区中的相同字符只会被再次读取,然后一次又一次地失败
如果您不验证您的输入,您可能对从该点开始处理有效输入没有信心,并且实际上可能会陷入未定义的行为。scanf函数族根据格式字符串中包含的格式说明符的数量返回有效转换的数量。因此,对于%d,您有一个格式说明符,例如%d,因此成功转换后,返回值将为1。如果用户使用Ctrl+d或windoze上的Ctrl+z手动取消输入,您还应检查EOF,这是留给您的
将这些部分放在一起,您可以执行以下操作:
#include <stdio.h>
#include <limits.h> /* for INT_MIN */
#define MAX 512
void input (int *scores, int num);
int findmax (int *scores, int num);
int findsum (int *scores, int num);
void display (int max, int sum);
int main (void)
{
int num, scores[MAX] = { 0 }, max, sum;
printf ("Enter number of scores: ");
if (scanf ("%d", &num) != 1) { /* VALIDATE ALL input */
fprintf (stderr, "error: invalid integer input.\n");
return 1;
}
input (scores, num);
max = findmax (scores, num);
sum = findsum (scores, num);
display (max, sum);
return 0;
}
void input (int *scores, int num)
{
int count = 0;
while (count < MAX && count < num) { /* always protect array bounds */
printf ("Enter score [%d]: ", count);
while (scanf ("%d", &scores[count]) != 1) { /* VALIDATE input */
fprintf (stderr, "error: invalid input, try again.\n");
/* remove invalid chars from input buffer */
for (int c = getchar(); c != '\n' && c != EOF; c = getchar()) {}
goto badval;
}
count++;
badval:;
}
}
int findmax (int *scores, int num)
{
int max = INT_MIN, count;
for (count = 0; count < num; count++)
if (scores[count] > max)
max = scores[count];
return max;
}
int findsum (int *scores, int num)
{
int sum = 0, count;
for (count = 0; count < num; count++)
sum = sum + scores[count];
return sum;
}
void display (int max, int sum)
{
printf ("\nThe max score is :%d\nThe total score is:%d\n", max, sum);
}
带纠错功能
如果您还有其他问题,请留下评论。分数[]={}->分数[1024/*假设输入的最大数量*/];在将数组传递到函数中时,还应传递数组的长度。在fedora终端中不起作用是什么意思。显示一些I/O设置问题寻求调试帮助为什么此代码不起作用?必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。
$ ./bin/maxminsum
Enter number of scores: 5
Enter score [0]: 9
Enter score [1]: 3
Enter score [2]: 7
Enter score [3]: 8
Enter score [4]: 2
The max score is :9
The total score is:29
$ ./bin/maxminsum
Enter number of scores: 5
Enter score [0]: 9
Enter score [1]: 3
Enter score [2]: foo
error: invalid input, try again.
Enter score [2]: 7
Enter score [3]: 8
Enter score [4]: bar
error: invalid input, try again.
Enter score [4]: 2
The max score is :9
The total score is:29