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)
@RawNewton
a
在运行
func
后,已在
main
中具有“新”值。您不需要从函数
func
返回任何值。您已将数组指针作为参数传递。
func
已在修改传递的数组。所以没有必要返回或分配任何内容。是的。在
func
函数中,我修改了值,现在我想将新值传递给
main
函数
a[]=func(a)->
func(a)
@RawNewton
a
在运行
func
后,已在
main
中具有“新”值。您不需要从函数
func
返回任何值。您已经将数组的指针作为参数传递。谢谢,现在就收到了。我返回数组时出错了。@RawNewton,对了,没有必要返回数组,Tanks现在得到了。我返回数组时出错了。@RawNewton,正确,没有必要返回数组