函数难度-C

函数难度-C,c,function,C,Function,我正在尝试编写一个函数来检查某个数字,如果它找到该数字,它会将其添加到总数中 #include <stdio.h> void unitSum(int input[], int output, int unit) { for (int n = 0; n < 5; n++) { if(input[n] == unit) output = output + unit; } } int main (void) {

我正在尝试编写一个函数来检查某个数字,如果它找到该数字,它会将其添加到总数中

#include <stdio.h>

void unitSum(int input[], int output, int unit)
{     
   for (int n = 0; n < 5; n++)
   {
      if(input[n] == unit)  
         output = output + unit;
   }
} 

int main (void)
{
    int array[5];
   int total = 0;

   for(int a = 0; a < 5; a++)
   {
    scanf("%d", &array[a]);
   }

   unitSum(array, total, 1);

 /*for (int n = 0; n < 5; n++)
   {
      if(array[n] == 1) 
         total = total + 1;
   }*/

    printf("%d", total);
}
#包括
无效单位和(整数输入[],整数输出,整数单位)
{     
对于(int n=0;n<5;n++)
{
if(输入[n]==单位)
输出=输出+单位;
}
} 
内部主(空)
{
int数组[5];
int-total=0;
对于(int a=0;a<5;a++)
{
scanf(“%d”和数组[a]);
}
单位和(数组,总计,1);
/*对于(int n=0;n<5;n++)
{
if(数组[n]==1)
总计=总计+1;
}*/
printf(“%d”,总计);
}
如果我使用输入“1 2”运行此程序,则得到的输出为0。但是,如果我取消注释底部的FOR循环,并注释掉函数调用。输出变为3(我想要)


我遗漏了一些简单的东西吗?

只需更改调用函数行unitSum(array,total,1)

to total=单位和(数组,总计,1);在函数unitSum中,改变

将类型返回到int,并在关闭for循环后返回输出

解决了

int unitSum(int input[], int output, int unit)
{     
   for (int n = 0; n < 5; n++)
   {
      if(input[n] == unit)  
      output = output + unit;
   }
   return output;
 } 
int-unitSum(int-input[],int-output,int-unit)
{     
对于(int n=0;n<5;n++)
{
if(输入[n]==单位)
输出=输出+单位;
}
返回输出;
} 

愉快的编码。

只需更改调用函数行unitSum(数组,总计,1)

to total=单位和(数组,总计,1);在函数unitSum中,改变

将类型返回到int,并在关闭for循环后返回输出

解决了

int unitSum(int input[], int output, int unit)
{     
   for (int n = 0; n < 5; n++)
   {
      if(input[n] == unit)  
      output = output + unit;
   }
   return output;
 } 
int-unitSum(int-input[],int-output,int-unit)
{     
对于(int n=0;n<5;n++)
{
if(输入[n]==单位)
输出=输出+单位;
}
返回输出;
} 

快乐编码。

C
中,参数是通过值传递的,而不是通过引用传递的,这意味着您的函数将复制变量
输出
,并仅使用副本进行处理,因此不会更改原始值。因此,如果您希望函数不在本地更改其中一个参数,则必须向其传递一个指针。
在您的代码中,这将修复:

// int *output is the pointer to an int variable
void unitSum(int input[], int *output, int unit)
{     
    for (int n = 0; n < 5; n++) {
        if(input[n] == unit)  
            // here you change the value of the variable that is 
            // located in this address in memory
            (*output) = (*output) + unit;
    }
} 

// ...

// &total is the pointer to variable total
unitSum(array, &total, 1);    
//int*输出是指向int变量的指针
无效单位和(整数输入[],整数*输出,整数单位)
{     
对于(int n=0;n<5;n++){
if(输入[n]==单位)
//在这里,您可以更改以下变量的值:
//位于内存中的此地址中
(*输出)=(*输出)+单位;
}
} 
// ...
//&total是指向变量total的指针
单位和(数组和总计,1);

C
中,参数是通过值传递的,而不是通过引用传递的,这意味着您的函数对变量
输出进行复制,并且仅使用副本进行处理,因此不会更改原始值。因此,如果您希望函数不在本地更改其中一个参数,则必须向其传递一个指针。
在您的代码中,这将修复:

// int *output is the pointer to an int variable
void unitSum(int input[], int *output, int unit)
{     
    for (int n = 0; n < 5; n++) {
        if(input[n] == unit)  
            // here you change the value of the variable that is 
            // located in this address in memory
            (*output) = (*output) + unit;
    }
} 

// ...

// &total is the pointer to variable total
unitSum(array, &total, 1);    
//int*输出是指向int变量的指针
无效单位和(整数输入[],整数*输出,整数单位)
{     
对于(int n=0;n<5;n++){
if(输入[n]==单位)
//在这里,您可以更改以下变量的值:
//位于内存中的此地址中
(*输出)=(*输出)+单位;
}
} 
// ...
//&total是指向变量total的指针
单位和(数组和总计,1);

问题在于函数
unitSum
不会更改
输出值。您应该调用
unitSum(array,&total,1)
,而不是
main
中的
unitSum(array,total,1)
。因此,如果我想编辑函数外部的变量值,我必须在变量名前加一个与号?编辑-这并没有解决它是肯定的。仅仅添加一个符号并不能解决这个问题。您必须进一步查看
指针
才能了解它的工作原理。@maffu:请不要编辑您的文章以包含“已解决”或类似内容。如果给出的其中一个答案特别有用,您可以,正如您阅读的介绍中所述。问题是您的函数
unitSum
不会更改
输出值。您应该调用
unitSum(array,&total,1)
,而不是
main
中的
unitSum(array,total,1)
。因此,如果我想编辑函数外部的变量值,我必须在变量名前加一个与号?编辑-这并没有解决它是肯定的。仅仅添加一个符号并不能解决这个问题。您必须进一步查看
指针
才能了解它的工作原理。@maffu:请不要编辑您的文章以包含“已解决”或类似内容。如果给出的答案中有一个特别有用,你可以,正如你在阅读的介绍性文章中提到的那样。