Assigning variables to pointers
- by tys
When compiling the below, the program seem to crash. However, there is no error in the compiling process.
...
int *x;
*x = 3;
printf("%d", *x);
...
From what I know, this program initializes the pointer *x to an integer value, and subsequently assigns the value of 3 to the deferenced pointer *x.
So why does the program crashes? If I do this instead, the program can work normally.
...
int *x, y;
y = 3;
x = &y;
printf("%d", *x);
...
So, what seems to be the problem with the skipping of the y variable, and instead, assigning the pointer *x directly to an integer value?