I am confused about how to use realloc
to add space for other elements in the array nums
when the program first starts it has space for two elements but if the user wants to add more elements it will cause a segfault, this means we need to create a bigger array with 3 or more elements and add them one by one for the old one until index 1 and take a user-provided integer for the third element in the array
If the program is supposed to run in a while loop which never ends unless the user kills the process means we have to use realloc
every time the array gets full that said my confusion starts here
do I have to make another array that will hold the address of realloc
since we need to free it later on or can it use the same pointer for multiple realloc
uses
int *nums[2];
int numsSize()
{
return sizeof(nums)/sizeof(int*);
}
//return index at which user added elements end
int numsIndex()
{
for (int i = 0 ; i < numsSize(); i++)
{
if (!nums[i])
{
return i;
}
}
return numsSize();
}
void numsResize()
{
// resize nums to have space for 4 elements
}
int main(void) {
nums[0] = 10 ;
printf("Size of array : %d \n", numsSize()); // outputs 2
printf("Index of last added element in array : %d\n", numsIndex()); // outputs 1
return 0;
}