I need to increase length of 2 arrays according to user input. I'm using the code below. But output is not matching with user input.
#include<stdio.h>
int main()
{
int i=0, key=0, size[key], time[key];
while (key!=-1)
{
printf("Insert value for size : ");
scanf("%d",&size[i]);
printf("Insert value for time : ");
scanf("%d",&time[i]);
i++;
printf("Run again Press-1 or Exit : ");
scanf("%d",&key);
}
int x=0;
for (x ; x<i ; x++)
{
printf("%d %d\n",size[x],time[x]);
}
return 0;
}
When user inputs the values:
35 4
20 3
40 1
60 7
20 8
Then this is the output:
8 4
20 3
40 1
60 7
20 8
If length of array exceeded 4, the last element of the time array will be printed as the first element of the size array.
Why does my program give this output?
realloc()if you want to allocate a resizeable array.reallocto change the size of memory allocated withmallocorcalloc.