你如何按值传递数组 [英] how do you pass an array by value

查看:61
本文介绍了你如何按值传递数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个函数foo(int arr []),它的原型

被声明为foo(int arr []);我在

函数中修改了数组的值,并且在主数组中修改了值,即

也被传递。我理解这种传递数组的方式是通过

值,如果原型被声明为foo(int *),那么它是通过

引用的,在这种情况下是值如果在函数中修改将获得

也反映在main函数中。我不明白为什么

值在主函数中被修改,尽管价值通过了

。数组传递值是否已经不再存在?

有问候,

Abhishek S

I wrote a function foo(int arr[]) and its prototype
is declared as foo(int arr[]); I modify the values of the array in the
function and the values are getting modified in the main array which
is passed also. I understand that this way of passing the array is by
value and if the prototype is declared as foo(int *), it is by
reference in which case the value if modified in the function will get
reflected in the main function as well. I dont understand why the
values are getting modified in the main function though there is pass
by value. Is pass by value for arrays not there any more?
With Regards,
Abhishek S

推荐答案

Abhi写道:
Abhi wrote:

我写了一个函数foo(int arr [])并且它的原型

被声明as foo(int arr []);我在

函数中修改了数组的值,并且在主数组中修改了值,即

也被传递。我理解这种传递数组的方式是通过

值,如果原型被声明为foo(int *),那么它是通过

引用的,在这种情况下是值如果在函数中修改将获得

也反映在main函数中。我不明白为什么

值在主函数中被修改,尽管价值通过了

。数组传递不再存在吗?
I wrote a function foo(int arr[]) and its prototype
is declared as foo(int arr[]); I modify the values of the array in the
function and the values are getting modified in the main array which
is passed also. I understand that this way of passing the array is by
value and if the prototype is declared as foo(int *), it is by
reference in which case the value if modified in the function will get
reflected in the main function as well. I dont understand why the
values are getting modified in the main function though there is pass
by value. Is pass by value for arrays not there any more?



从来没有数组。对于函数参数(和/ only /

为函数参数),作为数组类型的声明将

转换为声明作为指针类型。总是发生这种情况,并且无法绕过




void f(int * p){* p = 1; }


相当于


void f(int p []){* p = 1; }


当你调用一个函数,并尝试传递一个数组时,这个数组

将被转换为指向第一个元素的指针数组。


void g(void){int i [2]; F(1); }


相当于


void g(void){int i [2]; F(&安培; I [0]); }


解决方法是不使用数组作为函数参数。你可以

使用一个包含数组的结构。


struct Array {

int element [100];

} a;


void f(struct Array arr){arr.element [0] = 1; }


int main(无效){

a.element [0] = 0;

f(a);

返回a.element [0];

}


这将返回退出状态为0,因为f传递了一个复制

of a,所以a.element [0]仍未修改。

It was never there for arrays. For a function parameter (and /only/
for a function parameter), a declaration as an array type gets
converted to a declaration as a pointer type. This happens always, and
cannot be bypassed.

void f(int *p) { *p = 1; }

is equivalent to

void f(int p[]) { *p = 1; }

When you call a function, and you try to pass it an array, this array
will be converted to a pointer to the first element of the array.

void g(void) { int i[2]; f(i); }

is equivalent to

void g(void) { int i[2]; f(&i[0]); }

A workaround is to not use an array as a function parameter. You can
use a structure containing an array for that.

struct Array {
int element[100];
} a;

void f(struct Array arr) { arr.element[0] = 1; }

int main(void) {
a.element[0] = 0;
f(a);
return a.element[0];
}

This will return with an exit status of 0, because f is passed a copy
of a, so a.element[0] remains unmodified.


Abhi写道:
Abhi wrote:

我写了一个函数foo(int arr []),它的原型

被声明为foo(int arr []);我在

函数中修改了数组的值,并且在主数组中修改了值,即

也被传递。我理解这种传递数组的方式是通过

值,如果原型被声明为foo(int *),那么它是通过

引用的,在这种情况下是值如果在函数中修改将获得

也反映在main函数中。我不明白为什么

值在主函数中被修改,尽管价值通过了

。数组传递值是否已经不存在?

有问候,

Abhishek S
I wrote a function foo(int arr[]) and its prototype
is declared as foo(int arr[]); I modify the values of the array in the
function and the values are getting modified in the main array which
is passed also. I understand that this way of passing the array is by
value and if the prototype is declared as foo(int *), it is by
reference in which case the value if modified in the function will get
reflected in the main function as well. I dont understand why the
values are getting modified in the main function though there is pass
by value. Is pass by value for arrays not there any more?
With Regards,
Abhishek S



嗯,数组不能通过C中的值传递。你认为通过

的值不是这样的。实际传递给函数的是指向数组第一个元素的

指针。因此,您通过此指针修改数组的原始副本

。数组索引

表示法只不过是底层

指针算法的一种语法糖。


所以,你'当你说数组按值传递时,你会被误导。

实际上是将一个数组传递给一个函数的两种形式,你上面显示的是b
,几乎完全相同。

Well, arrays can''t be passed by value in C. What you believe as pass
by value is not so. What is actually passed to the function is a
pointer to the first element of the array. Therefore you modify the
original copy of the array through this pointer. The array indexing
notation is nothing but a form of syntactic sugar for the underlying
pointer arithmetic.

So, you''re misinformed when you say that arrays are passed by value.
In fact the two forms of passing an array to a function, which you''ve
shown above, are virtually identical.




" Abhi" < ab ************* @ gmail.comha scritto nel messaggio

新闻:11 **************** ******:p15g2000hsd.googlegr oups.com ...

"Abhi" <ab*************@gmail.comha scritto nel messaggio
news:11**********************@p15g2000hsd.googlegr oups.com...

我写了一个函数foo(int arr [])及其原型

声明为foo(int arr []);我在

函数中修改了数组的值,并且在主数组中修改了值,即

也被传递。我理解这种传递数组的方式是通过

值,如果原型被声明为foo(int *),那么它是通过

引用的,在这种情况下是值如果在函数中修改将获得

也反映在main函数中。我不明白为什么

值在主函数中被修改,尽管价值通过了

。数组传递值是否已经不存在?

有问候,

Abhishek S
I wrote a function foo(int arr[]) and its prototype
is declared as foo(int arr[]); I modify the values of the array in the
function and the values are getting modified in the main array which
is passed also. I understand that this way of passing the array is by
value and if the prototype is declared as foo(int *), it is by
reference in which case the value if modified in the function will get
reflected in the main function as well. I dont understand why the
values are getting modified in the main function though there is pass
by value. Is pass by value for arrays not there any more?
With Regards,
Abhishek S



尝试使用malloc和memcpy复制数组,在副本上执行操作

,然后释放它。

BTW我想你需要传递数组的长度作为参数,因为

否则函数无法知道数组有多长。

(int arr []只传递& arr [0] ,期间。)

Try using malloc and memcpy to duplicate the array, perform the operations
on the copy, and then free it.
BTW I guess you need to pass the length of the array as a parameter, because
otherwise the function won''t have any way to know how long the array is.
(int arr[] only passes &arr[0], period.)


这篇关于你如何按值传递数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆