-1

//I want to revert array back to original state after 2nd printf statement OR code any other way such that next code a=++(*p) operates on origianl array not on Update array.

CODE:

#include <stdio.h>
#include <stdlib.h>

int main(){
    int arr[]={10,20,30,40,50};   
    int *p = arr;    
    int a;

    printf("\n arr[0] = %d, arr[1] = %d, a = %d, *p = %d", arr[0], arr[1],a , *p);

    a= ++*p;
    printf("\n arr[0] = %d, arr[1] = %d, a = %d, *p = %d", arr[0], arr[1],a , *p);
    
    //I WANT TO RESET ARRAY BACK TO ORIGINAL STATE after this printf statement or any other way such that next code a= ++(*p) operates on origianl array not on Update array.
    
    a= ++(*p);
    printf("\n arr[0] = %d, arr[1] = %d, a = %d, *p = %d", arr[0], arr[1],a , *p);  

}

OUTPUT GIVEN:


 arr[0] = 10, arr[1] = 20, a = 32767, *p = 10
 arr[0] = 11, arr[1] = 20, a = 11, *p = 11
 arr[0] = 12, arr[1] = 20, a = 12, *p = 12┌─[user@user]─[~/Documents/vs_code/DS/TEMP]
└──╼ $

OUTPUT EXPECTED:


 arr[0] = 10, arr[1] = 20, a = 32767, *p = 10
 arr[0] = 11, arr[1] = 20, a = 11, *p = 11
 arr[0] = 11, arr[1] = 20, a = 11, *p = 11┌─[user@user]─[~/Documents/vs_code/DS/TEMP]
└──╼ $
ayushkadbe
  • 91
  • 2
  • 13
  • Anything wrong with `arr[0] = 10`? Seems straightforward, but maybe there is more to the question than you are letting on – john Jun 26 '22 at 11:47
  • I mean you could subtract like `--*p`, but maybe you are looking for something more generic? – AndyG Jun 26 '22 at 12:05
  • 1
    There's nothing in C++ that will do this for you, automatically, you will have to implement this logic yourself: copy the original array values somewhere else, then copy them back when you're done fiddling with the array. – Sam Varshavchik Jun 26 '22 at 12:10
  • There are no 2 arrays, just a single one and a pointer referring to the same data. In this case the simplest solution would simply be not to modify the array at all by using `a = *p + 1; printf("\n arr[0] = %d, arr[1] = %d, a = %d, *p = %d", a, arr[1], a , a);` but if you want to apply arbitrary modifications that may not be as easy to revert, simply create a copy of the array. Using `std::array` instead of `int[5]` as type for `arr` simplifies this, since `std::array` is copyable. – fabian Jun 26 '22 at 12:11
  • Your question seems to be more about "C" then "C++" (include of stdio.h and stlib.h). C and C++ are different languages. In C++ for example it would make more sense to use std::array/std:vector. And you could use std::stack> push to store the state and first() + pop() to restore it. C++ in general relies a lot more on abstractions and less on pointers then C. And ofcourse the std::stack approach might use more memory the you would want to, but that's not clear from your problem statement (you could always use a stack to push individual changes to the array) – Pepijn Kramer Jun 26 '22 at 12:16
  • Do not tag both C and C++ except when asking about differences or interactions between the two languages. Pick one language and delete the other tag. – Eric Postpischil Jun 26 '22 at 13:24
  • Thanks for your answers fabian & pepijn-kramer. This might work for c++. – ayushkadbe Jun 27 '22 at 04:28

2 Answers2

0

Stop writing C code and learn modern C++.

You can use std::array, which has value semantics. This allows you to simply copy the array and copy it back later if that is what you want:

#include <cstdio>
#include <cstdlib>
#include <array>

int main(){
    std::array arr{10,20,30,40,50};   
    int *p = &arr[0];   
    int a = 0;

    printf("\n arr[0] = %d, arr[1] = %d, a = %d, *p = %d", arr[0], arr[1],a , *p);

    std::array arr2 = arr;
    a= ++*p;
    printf("\n arr[0] = %d, arr[1] = %d, a = %d, *p = %d", arr[0], arr[1],a , *p);
    
    //I WANT TO RESET ARRAY BACK TO ORIGINAL STATE after this printf statement or any other way such that next code a= ++(*p) operates on origianl array not on Update array.
    arr = arr2;

    a= ++(*p);
    printf("\n arr[0] = %d, arr[1] = %d, a = %d, *p = %d", arr[0], arr[1],a , *p);  

}

Modifying the copy would have been better.

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
0

Thanks for logic: @sam-varshavchik

Copy the original array values somewhere else, then copy them back when you're done fiddling with the array.

#include <stdio.h>
#include <stdlib.h>

int main(){
    int arr[]={10,20,30,40,50};   
    int *p = arr;    
    int a;

    int length= sizeof(arr)/sizeof(arr[0]);

    int arr2[length];

    for(int i=0; i<length;i++){
        arr2[i]= arr[i];
    }


    printf("\n arr[0] = %d, arr[1] = %d, a = %d, *p = %d", arr[0], arr[1],a , *p);

    a= ++*p;
    printf("\n arr[0] = %d, arr[1] = %d, a = %d, *p = %d", arr[0], arr[1],a , *p);   
    
    p= &arr2;
    a= ++(*p);
    printf("\n arr[0] = %d, arr[1] = %d, a = %d, *p = %d", arr[0], arr[1],a , *p);

    
}
ayushkadbe
  • 91
  • 2
  • 13