Realloc

PHOTO EMBED

Sun May 14 2023 19:51:02 GMT+0000 (Coordinated Universal Time)

Saved by @prachi

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

int main() {
  int n, i;
  int *array1, *array2;

  printf("Enter the size of the arrays: ");
  scanf("%d", &n);

  array1 = (int *) malloc(n * sizeof(int));
  array2 = (int *) malloc(n * sizeof(int));

  printf("Enter elements for array 1:\n");
  for (i = 0; i < n; i++) {
    scanf("%d", &array1[i]);
  }

  printf("Enter elements for array 2:\n");
  for (i = 0; i < n; i++) {
    scanf("%d", &array2[i]);
  }

  printf("Adding the two arrays...\n");
  for (i = 0; i < n; i++) {
    array1[i] += array2[i];
  }

  printf("The result array is:\n");
  for (i = 0; i < n; i++) {
    printf("%d ", array1[i]);
  }
  printf("\n");

  printf("The address of the result array is: %p\n", array1);

  printf("Enter the new size for the array: ");
  scanf("%d", &n);

  array1 = (int *) realloc(array1, n * sizeof(int));

  printf("The new address of the array is: %p\n", array1);

  free(array1);
  free(array2);
  return 0;
}
content_copyCOPY