Arrays 如何从另一个函数中检索c中的数组值
这是我的密码:Arrays 如何从另一个函数中检索c中的数组值,arrays,c,Arrays,C,这是我的密码: #include <stdio.h> int func(int a[]); int main() { int a[5] = {1, 2, 3, 4, 5}; a[] = func(a); #包括 int func(int a[]); int main() { inta[5]={1,2,3,4,5}; a[]=func(a); 为了在数组中获得新值,我应该在上面的行上做什么更改 for (i = 0; i < 5; i++)
#include <stdio.h>
int func(int a[]);
int main()
{
int a[5] = {1, 2, 3, 4, 5};
a[] = func(a);
#包括
int func(int a[]);
int main()
{
inta[5]={1,2,3,4,5};
a[]=func(a);
为了在数组中获得新值,我应该在上面的行上做什么更改
for (i = 0; i < 5; i++)
{
printf("%d ", a[i]);
}
}
int func(int a[])
{
for (i = 0; i < 5; i++)
{
a[i] = a[i] + 1;
}
return a;
}
(i=0;i<5;i++)的
{
printf(“%d”,a[i]);
}
}
int func(int a[]
{
对于(i=0;i<5;i++)
{
a[i]=a[i]+1;
}
返回a;
}
提前谢谢 当您将数组作为函数的参数传递时,它不是一个副本,事实上,该参数使用
int func(int a[])衰减为指向传递数组的第一个元素的指针
与使用int func(int*a)基本相同代码>,对函数内的数组所做的任何更改都将是永久性的
为了在数组中获得新值,我应该在上面的行上做什么更改
根据上述解释,函数不需要返回a
:
void func(int a[]) // as no return is needed, the return type should be void
{
for (int i = 0; i < 5; i++)
{
a[i] = a[i] + 1;
}
// no need to return a, it's permanently changed already
}
输出将是:
2 3 4 5 6
当您将数组作为函数的参数传递时,它不是一个副本,事实上,该参数使用int func(int a[])衰减为指向传递数组的第一个元素的指针
与使用int func(int*a)基本相同代码>,对函数内的数组所做的任何更改都将是永久性的
为了在数组中获得新值,我应该在上面的行上做什么更改
根据上述解释,函数不需要返回a
:
void func(int a[]) // as no return is needed, the return type should be void
{
for (int i = 0; i < 5; i++)
{
a[i] = a[i] + 1;
}
// no need to return a, it's permanently changed already
}
输出将是:
2 3 4 5 6
func
已在修改传递的数组。所以没有必要返回或分配任何内容。是的。在func
函数中,我修改了值,现在我想将新值传递给main
函数a[]=func(a)代码>->func(a)
@RawNewtona
在运行func
后,已在main
中具有“新”值。您不需要从函数func
返回任何值。您已将数组指针作为参数传递。func
已在修改传递的数组。所以没有必要返回或分配任何内容。是的。在func
函数中,我修改了值,现在我想将新值传递给main
函数a[]=func(a)代码>->func(a)
@RawNewtona
在运行func
后,已在main
中具有“新”值。您不需要从函数func
返回任何值。您已经将数组的指针作为参数传递。谢谢,现在就收到了。我返回数组时出错了。@RawNewton,对了,没有必要返回数组,Tanks现在得到了。我返回数组时出错了。@RawNewton,正确,没有必要返回数组