Final answer:
The statement int *p = 10; assigns the pointer p to an invalid memory address, causing a segmentation fault. It should instead point to a valid integer variable, like int value = 10; int *p = &value; which is then safe to dereference and print.
Step-by-step explanation:
The statement int *p = 10; in C does not do what you might intuitively expect. It sets the pointer p to the address 10 in memory, which is not a valid location for the program to access, hence resulting in a crash or segmentation fault. If you want to assign the value 10 to a location in memory and then print it, you should use the following code:
int main() {
int value = 10;
int *p = &value;
printf("%d", *p); // prints 10
return 0;
}
Here,
p points to the address of variable
value, which contains the integer 10. The
printf function can then safely dereference
p to print its value.