I was writing a simple program and created an array to store 4 integers. Then I used a for
loop to assign them; right after that I used the gets()
function to get a string; after using the gets()
function the first integer on the array would always become a 0. I even printed the variable on the screen before and after the gets()
to confirm.
The only thing that fixed it was dynamically allocating the array, so now I want to know if I should always allocate arrays dynamically to prevent this kind of issue?
code:
int nums[4];
int i = 0;
char symbols[3];
for(i=0;i<4;i++){
scanf("%d", &nums[i]);
}
fflush(stdin);
gets(symbols);
calculate(nums, symbols);