【发布时间】: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 s。s根本没有使用。 -
有return语句但我把它写成注释行