Q:

How to undo changes to an array after passing to a function

Is there any way to undo the actions or get the original array after I changed the array as shown below.
#include <stdio.h>

void function(int array[]){

    array[2] = 20;

    //do some extra work
    return;
}

int main(void){

    int array[5] = {1,2,3,4,5};

    function(array);

    // code which has to use original array 

    return 0;
}

A:

You can pack two 32 bit integers (old / new) into a 64 bit integer, example:
#include <stdio.h>
#include <stdint.h>

void function(int64_t array[])
{
    array[2] = (array[2] << 32) | 20;
}

void printarr(int64_t array[], size_t n)
{
    size_t i;

    for (i = 0; i < n; i++) {
        printf("%d ", (int32_t)(array[i]));
    }
    printf("\n");
}

int main(void)
{
    int64_t array[] = {1, 2, 3, 4, 5};
    size_t i, n = sizeof(array) / sizeof(array[0]);

    function(array);
    puts("After function:");
    printarr(array, n);
    for (i = 0; i < n; i++) {
        if (array[i] >> 32 != 0) /* Changed */
            array[i] = array[i] >> 32; /* Undo */
    }
    puts("Original values:");
    printarr(array, n);
    return 0;
} 

Output:
After function:
1 2 20 4 5 
Original values:
1 2 3 4 5 

Note:
Of course you can pack two 16 bit integers in a 32 bit integer if you are using short values in order to save some space.
To be portable use PRId32 format (defined in <inttyes.h>) for printf and int32_t:
printf("%"PRId32" ", (int32_t)x);

Another method:
If those changes are made sequentially over positive integers you can change the sign (to identify a change) and store only the changes using realloc:
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int *value;
    size_t length;
} t_undo;

void function(t_undo *undo, int array[], int index, int value)
{
    undo->value = realloc(undo->value, sizeof(int) * (undo->length + 1));
    /* check realloc */
    undo->value[undo->length++] = array[index];
    array[index] = -value;
}

void printarr(int array[], size_t n)
{
    size_t i;

    for (i = 0; i < n; i++) {
        printf("%d ", abs(array[i]));
    }
    printf("\n");
}

int main(void)
{
    t_undo *undo;
    int array[] = {1, 2, 3, 4, 5};
    size_t i, j = 0, n = sizeof(array) / sizeof(array[0]);

    undo = malloc(sizeof(*undo));
    /* check malloc */
    undo->value = NULL;
    undo->length = 0;
    function(undo, array, 2, 20);
    puts("After function:");
    printarr(array, n);
    for (i = 0; i < n; i++) {
        if (array[i] < 0) /* Changed */
            array[i] = undo->value[j++]; /* Undo */
    }
    puts("Original values:");
    printarr(array, n);
    free(undo->value);
    free(undo);
    return 0;
} 

