Anyone have any idea why this works:
int main(void)
{
char action;
int *i;
*i=10;
printf("%d",*i);
action='C';
printf("%c",action);
}
but this doesnt
int main(void)
{
char action='C';
int *i;
*i=10;
printf("%d",*i);
printf("%c",action);
}
I am getting no errors but it will not run! cant see why that little difference makes it crash
It’s
undefined bahavior.You have created a pointer and without initializing the pointer, you are directly putting the value
10to the memory location pointed to by pointeriwhereihasindeterminate value.So, in that case it will put the value10to that garbage value location (the value of pointeri). So it may execute if garbage value location is accessible or may crash if memory location is not accessible.Hence it’s just
undefined bahavior