Memory allocation with realloc() | C

September 27th, 2021 01:27:10 PM

					
#include <stdio.h> #include <stdlib.h> int main() { int *ptr, i , n1, n2; printf("Enter size: "); scanf("%d", &n1); ptr = (int*) malloc(n1 * sizeof(int)); printf("Addresses of previously allocated memory:\n"); for(i = 0; i < n1; ++i) printf("%pc\n",ptr + i); printf("\nEnter the new size: "); scanf("%d", &n2); // rellocating the memory ptr = realloc(ptr, n2 * sizeof(int)); printf("Addresses of newly allocated memory:\n"); for(i = 0; i < n2; ++i) printf("%pc\n", ptr + i); free(ptr); return 0; }

Featured Posts