【问题标题】:C programming ... Why "return" statement give only one value in this code? [closed]C 编程...为什么“return”语句在这段代码中只给出一个值? [关闭]
【发布时间】:2026-01-17 04:40:02
【问题描述】:

我是 C 编码的新手。我正在尝试编写一个程序,该程序在数组的第一个位置添加一个值。如下所示,append 函数必须返回多个值。

当我在这个函数中使用printf("%d ", array[c]) 语句时,没有问题,我得到了我想要的值。

但是,当我在这个函数中使用return s; 语句时,它只给出一个值,尽管它必须给出与printf("%d ", array[c]) 语句相同的值。

当我使用printf("%d ", array[c]) 运行此代码时,输​​出为:

25、5、8、555、99

当我用return s 运行这段代码时,输​​出是;

25

为什么两个语句之间存在差异?我不需要在屏幕上打印值。我需要使用return 声明。请帮帮我……

#include <stdio.h>

int append(int array[]){

          int  position, c, s, len=4, value=25; 

          position=1;

          for (c = len - 1; c >= position-1; c--){
              array[c+1] = array[c];
              }
          array[position-1] = value;
          for (c = 0; c <= len; c++){

                //return array[c]; // <-- why this give only one value???   
                printf("%d ", array[c]);    //<--  this print the all value !!! 
              }          
}

int main(){

    int kar[]= {5, 8, 555, 99};

    printf("%d", append(kar));

    return 0;
}

【问题讨论】:

  • 这里append函数中没有return语句。
  • 为什么append 需要返回任何东西?可能你应该只改变数组吗?我怀疑你遗漏了很多基础知识,解释所有基础知识超出了一个问题的答案范围。
  • 没有return ss 根本没有使用。
  • 有return语句但我把它写成注释行

标签: c return


【解决方案1】:

您将array 传递给函数,该函数实际上是将指针传递给数组的第一个元素。这意味着函数中数组的更改也会出现在调用函数中,因为它是同一个数组。

您无需退货。只需打印main 中的数组内容即可。

此外,一个大小为 4 的数组的索引从 0 到 3。现在您正在写入数组的末尾。您需要更改您的功能以防止这种情况发生。您不能只写到数组的末尾以使其更大。大小是固定的。

void append(int array[]){
      int  position, c, s, len=4, value=25; 
      position=1;

      for (c = len - 2; c >= position-1; c--){
          array[c+1] = array[c];
      }
      array[position-1] = value;
}

int main() {
    int kar[]= {5, 8, 555, 99};

    append(kar);

    int c;
    for (c = 0; c < 4; c++){
        printf("%d ", kar[c]);
    }       
    return 0;
}

【讨论】:

  • 感谢您的回复,但代码给出:[错误] 'array' 未在此范围内声明
  • @Kemal 那应该是kar。已更新。
  • #include void append(int array[]){ int position, c, s, len=4, value=25;位置=1; for (c = len - 2; c >= position-1; c--){ array[c+1] = array[c]; } 数组[位置-1] = 值; } int main() { int kar[]= {5, 8, 555, 99};附加(卡尔);诠释 c; for (c = 0; c
  • 对不起,但它给出了同样的错误......
  • 谢谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!好的。对不起,这是我的错误......再次感谢......
【解决方案2】:

在 C 中,每个函数必须只返回一个值。 (如果不需要返回任何东西,可以是void。)

如果你想从一个函数中得到多个结果,有两种方法可以实现。

选项一: 在您的函数中,分配某种结构来保存多个值(通常但不一定是数组)并让 one 从您的函数是指向该结构的 指针。函数的调用者通常会负责清理该结构,例如使用free

选项二:让您的函数获取一个指向多个值的某种容器(通常但不一定是数组)的指针,并通过将结果添加到容器中来“返回”结果.

在任何一种情况下,调用者(在这种情况下为main)仍然有责任对每个值做一些有意义的事情,例如使用for 循环将它们全部打印出来。

【讨论】:

  • 返回包含两个值的结构(不是指向结构的指针)更简单。
【解决方案3】:

当我用 "printf("%d ", array[c])" 运行这段代码时,输​​出是;

25、5、8、555、99

那是因为您将printf 语句作为循环的一部分来调用:

for (c = 0; c <= len; c++){
  printf("%d ", array[c]);    
}       

IOW,你在打电话

printf("%d ", array[0]); // prints 25
printf("%d ", array[1]); // prints 5
printf("%d ", array[2]); // prints 555
printf("%d ", array[3]); // prints 99

printf%d 修饰符不能打印整个 int 数组 - 它一次只能打印一个 int 值。

请注意您的 return 声明

return array[c];

只返回数组的一个单个元素(在循环内,即元素 0)。

正如其他人所提到的,您不需要返回任何东西(可能除了新的数组大小)。

但这带来了另一个问题 - 鉴于您的声明

int kar[]= {5, 8, 555, 99};

kar 不能容纳超过 4 个值 - 它不能扩展为容纳更多。您要么需要声明 kar 的大小大于 4:

int kar[10] = {5, 8, 555, 99};

或者您将需要动态分配该内存并根据需要对其进行扩展:

size_t arr_size = 4;
int *kar = malloc( sizeof *kar * arr_size );

kar[0] = 5;
kar[1] = 8;
kar[2] = 555;
kar[3] = 99;

/**
 * Add 25 to the beginning of the array
 */
append( 25, &kar, &arr_size );

然后在你的append 函数中:

void append( int val, int **array, size_t *size )
{
  /** 
   * Extend the array size by 1.  We assign the result to a temporary in 
   * case realloc fails and returns a NULL - this way we won't lose our 
   * reference the the original memory.  
   *
   * In practice, we would not extend the array one element at a time (realloc
   * can be an expensive operation, so you want to minimize the number of
   * times you call it), but I'm trying to keep the example as simple as
   * I can.  
   */
  int *tmp = realloc( *array, *size + 1 );

  if ( tmp )
  {
    /**
     * Realloc succeeded - update the size and assign tmp back to
     * *array (this is why we needed to pass `array` as an `int **`;
     * we need to update the pointer value).  
     */
    (*size)++;
    *array = tmp;

    /**
     * Shift all elements up one.
     */
    for ( size_t c = *size - 1; c > 0; c-- )
      (*array)[c] = (*array)[c-1];           // we don't want to index into array, we want
                                             // to index into what array *points to*
    /**
     * Add the new value to the beginning of the array
     */
    (*array)[0] = val;
  }
  else
  {
    fprintf( stderr, "Could not extend input array, contents left unchanged\n" );
  }
}

完成后,请务必在main 末尾释放该内存:

free( kar );

【讨论】:

  • 感谢您的详细解答。对不起,但是当我按照你说的重新排列我的代码时,我遇到了这个错误:[错误]从'void *'到'int *'的无效转换[-fpermissive]。 int *tmp = realloc( *array, *size + 1 );此行导致错误。
  • @Kemal:你可能需要#include &lt;stdlib.h&gt;