I am a beginner coder in C and I recently learned the concept of dynamic arrays using calloc/malloc in C but I seem to have ran in a program.
This program right here asks for 10 elements from the user and then asks if you want to add more into it? depending on the value entered, realloc function is supposed to reallocate more memory for further elements, but the code just terminates when it reaches the realloc statement. Please help.
#include<stdio.h>
#include<stdlib.h>
int* arr;
size_t arrSize = 10;
int main(){
int modSize;
printf("The Program is making a dynamic array\n");
arr = calloc(arrSize,sizeof(int));
if(arr == NULL){
printf("error from calloc\n");
}
printf("Enter Values for the Array: \n");
for(int i=0; i<arrSize; ++i){
scanf("%d",&arr[i]);
}
printf("Would you like to add more values? If yes type the amount : ");
scanf("%d",&modSize);
arrSize += modSize;
arr = realloc(arrSize,arrSize*sizeof(int));
if(arr == NULL){
printf("error from realloc\n");
}
for(int i = 10; i<arrSize; ++i){
scanf("%d",&arr[i]);
}
for(int i = 0; i<arrSize; ++i){
printf("%d\t",arr[i]);
}
free(arr);
return 0;
}