-
*((int*)2000)
is not “perfectly valid code”. The result of converting an integer to a pointer is implementation-defined. It’s likely that in your implementation,(int*)2000
results in an invalid pointer. Trying to dereference an invalid pointer produces undefined behavior, which means that anything can happen. When you ran the program with theprintf
line uncommented, it happened to result in a segmentation violation, because the resulting pointer pointed to unaccessible memory. If you’d used some other integer, it might have resulted in a valid pointer, and then you’d see the contents of that memory location. -
All the parameters to a function call have to be evaluated before the function is called. The above error is happening while evaluating the parameters to
printf()
, so the program stops before the function is called. As a result, nothing is printed.
solved What’s wrong with this code which prints the value at address obtained after converting an integer to an integer pointer [closed]