简体   繁体   English

如何以我们可以操作但更改不应反映在主函数内部的方式传递数组(或指针)?

[英]How to pass array(or pointer) in such a way that we can manipulate but the change should not reflect inside main function?

I want any int array inside main() function in such a way that I can not manipulate it inside the main() function but I can pass it to the other function to make some changes and again I don't want the reflection of that change in the array of main function?我想要main()函数中的任何int数组,这样我就无法在main()函数中对其进行操作,但我可以将它传递给另一个函数以进行一些更改,并且我不希望反映它main函数数组的变化? Is there some way apart from copying?除了复制还有什么办法吗? Please suggest me.请给我建议。
Here is a sample program:这是一个示例程序:

#include<stdio.h>
void fun( int *arr);
int main()
{
    const int a[]={1,2,10,20};
     
     //a[3]=42;        I can not do as the array is read only
     
    fun(a);
    printf("%d ",a[3]);  //the change is reflected here and all I want is not to reflect any change here
    
    return 0;
}

void fun(int *arr)
{
    *(arr+3)=42;    //doing this I want change only inside this function
}

The output is **42**. 输出为 **42**。 All I want is the output **20** ie ```a[3]``` of ```main()```function 我想要的只是输出**20**,即```main()```函数的```a[3]```

No. C is a low-level language, so what you see is what you get.不,C 是一种低级语言,所以所见即所得。 Meaning if you are manipulating a data area (ie changing memory contents) the only way to not affect an "original" is to be manipulating a copy.这意味着如果您正在操作数据区域(即更改内存内容),不影响“原始”的唯一方法是操作副本。

Is there some way apart from copying?除了复制还有什么办法吗?

Not really.并不真地。 Code could reduce copying by restoring select array elements.代码可以通过恢复选择数组元素来减少复制。

void fun(int *arr) {
  int orignal_3 = *(arr+3);

  *(arr+3)=42;    //doing this I want change only inside this function
  // ...

  *(arr+3) = orignal_3;
}

Note that OP's current code has undefined behavior due to const in const int a[]={1,2,10,20};请注意,由于const const int a[]={1,2,10,20};中的 const,OP 的当前代码具有未定义的行为; and fun() *(arr+3)=42;fun() *(arr+3)=42; . . Best to compile with all warnings enabled.最好在启用所有警告的情况下进行编译。

error: passing argument 1 of 'fun' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]

I want any int array inside main() function in such a way that I can not manipulate it inside the main() function but I can pass it to the other function to make some changes and again I don't want the reflection of that change in the array of main function?我想要main()函数中的任何int数组,这样我就无法在main()函数中对其进行操作,但我可以将它传递给另一个函数以进行一些更改,并且我不希望反映它main函数数组的变化?

Consider it at a higher level.在更高的层次上考虑它。 You want to modify an object.你想修改一个对象。 No matter where you do this, all observers of that object will be able to see the changes.无论您在哪里执行此操作,该对象的所有观察者都将能够看到更改。 Therefore, if main 's object must not be modified then no function may modify it.因此,如果main的对象不能被修改,那么任何函数都不能修改它。 If another function wants a similar object that it can modify, then that object must be a copy.如果另一个函数想要一个可以修改的类似对象,那么该对象必须是一个副本。

Is there some way apart from copying?除了复制还有什么办法吗?

No.不。

Perhaps the question is inspired by C's pass-by-value semantics, which have the result that functions automatically receive copies of the arguments presented by the caller.也许这个问题是受到 C 的按值传递语义的启发,其结果是函数会自动接收调用者提供的参数的副本。 The fine point surrounding arrays in this area is that you cannot pass an array as a function argument at all .该区域中围绕数组的要点是您根本不能将数组作为函数参数传递。 That concept cannot be expressed in C, because array-valued expressions are automatically converted to pointers in almost all contexts where they appear, including function calls' argument lists.这个概念不能用 C 来表达,因为数组值表达式在它们出现的几乎所有上下文中都会自动转换为指针,包括函数调用的参数列表。 The function receives a copy of the pointer in such cases, and that points to the storage that the original array occupies.在这种情况下,该函数接收指针的副本,并指向原始数组占用的存储空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM