I ran into this issue in a bigger program I was making, so I tried to boil down the issue to a lot less code.
The issue is that I have a while loop, that runs and takes some user input, and then terminates or keeps running based on the input of the user. My example looks like this:
int main() {
int running = 0;
while (running == 0){
int n;
fflush(stdin);
scanf("%d", n);
printf("n is %d \n", n);
if (n == 0){
running =1;
}
}
}
And whatever input I give, this fails in the following way:
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
I understand that this problem often happens when you mess up some memory adresses or some pointers. But I really can't see what's going wrong here though, I only have two variables, and I'm deleting anything.
Can someone help me
CodePudding user response:
Two errors in 2 lines.
- Major:
scanf("%d", n);
scanf
requires pointer you pass integer. Undefined behaviour fflush(stdin);
- it is UB (Undefined Behaviour) too