Home > Back-end >  How to pass array(or pointer) in such a way that we can manipulate but the change should not reflect
How to pass array(or pointer) in such a way that we can manipulate but the change should not reflect

Time:07-07

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** i.e ```a[3]``` of ```main()```function

CodePudding user response:

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.

CodePudding user response:

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]

CodePudding user response:

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.

  • Related