Search code examples
cmemory-managementsummallocrealloc

increase size of array


May you help me with this exercise please?

Write a C program that reads 6 integers from the keyboard and assigns the first 5 values at the first 5 positions of an array; store the sixth value in a variable N. Write a function that, given input the array initialized with the first 5 values from the keyboard and the integer N, returns the array resized to contain 5 + N elements, such that each one of the new N elements corresponds to the sum of the numbers before it in the array. In main, print the content of the array returned by the function.

It's OK also all in the main function.

I have the problem when I have to use the function realloc to increment the array from size = 5 to 5 + N.

This is my code:

int N, a, i;
int *ptr;
int arr[6];

for (i = 0; i < 5; i++) {
    printf("Insert number in array, position(%d): ", i);
    scanf("%d", &arr[i]);
}

N = arr[4];

a = 5 + N;

ptr = (int *)realloc(arr, sizeof(int) * a);

for (i = 4; i < a; i++) {
    ptr + i = N * N; //<--- **problem!!**
}

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

free(ptr);

Solution

  • You cannot reallocate an array defined locally in a function nor defined globally. You can only call realloc on an object previously allocated with malloc(), calloc() or realloc() or with a NULL pointer. So you must allocate the initial array with 5 elements in main() and reallocate it in the function.

    #include <stdio.h>
    #include <stdlib.h>
    
    int *extend_array(int *arr, int N) {
        int a = 5 + N;
        arr = realloc(arr, a * sizeof(int));
        if (arr != NULL) {
            int sum = 0;
            for (int i = 0; i < 5; i++) {
                sum += arr[i];
            }
            for (int i = 5; i < a; i++) {
                arr[i] = sum;
                sum += sum;
            }
        }
        return arr;
    }
    
    int main() {
        int N;
    
        int *arr = malloc(5 * sizeof(int));
        if (arr == NULL) {
            printf("allocation failed\n");
            return 1;
        }
        for (int i = 0; i < 5; i++) {
            printf("Insert number in array, position(%d): ", i);
            if (scanf("%d", &arr[i]) != 1) {
                printf("invalid input\n");
                return 1;
            }
        }
    
        printf("Insert the value of N: ");
        if (scanf("%d", &N) != 1) {
            printf("invalid input\n");
            return 1;
        }
    
        int *ptr = extend_array(arr, N);
        if (ptr == NULL) {
            printf("reallocation failed\n");
        } else {
            arr = ptr;
            for (int i = 0; i < 5 + N; i++) {
                printf("%d\n", arr[i]);
            }
        }
        free(arr);
        return 0;
    }
    

    The assignment specifies that the function should take the array and the number N as arguments, but it would be better to make the initial size a variable and pass that to the function as well to make the code more generic, easier to extend, and less error prone as the constant 5 appears in many places.