简体   繁体   中英

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? 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**. All I want is the output **20** ie ```a[3]``` of ```main()```function

No. C is a low-level language, so what you see is what you get. 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}; and 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?

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. 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. 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. The function receives a copy of the pointer in such cases, and that points to the storage that the original array occupies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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