When we create an array with unknown size then we use malloc() function.
Here is the code when I want to take the array size from user as an input.
int* ptr, len;
printf("Please enter the size number:");
scanf_s("%d", &len);
ptr = (int*)malloc(len * sizeof(int));
for (int i = 0; i < len; i++)
{
printf("Enter the %d. number: ", i+1);
scanf_s("%d", ptr + i);
}
But here is the question I want to build an application where user doesn't indicate any size value and enters the numbers so that place them into an array. Array is filling but hasn't got any limits. It hasn't allocated any memory initially like my code above. Only limit is that user enters a specific number (say -5) then, array is being stopped. And prints out the values.
In essence: I am looking for memory allocation but allocation will be decided on specific user input.
Realloc edit that runs the code infnitely and never displays the array
int i = 0,ctr=0;
int* ptr = (int*)malloc(sizeof(int));
do
{
printf("Enter the %d. value: \n",i+1);
scanf_s("%d", ptr + i);
ctr += 1;
ptr = (int*)realloc(ptr, (i + 2) * sizeof(int));
i += 1;
} while (*(ptr+i)!=-1);