Assigning variables to pointers

Posted by tys on Stack Overflow See other posts from Stack Overflow or by tys
Published on 2014-06-09T03:18:01Z Indexed on 2014/06/09 3:25 UTC
Read the original article Hit count: 77

Filed under:

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?

© Stack Overflow or respective owner

Related posts about c