Segmentation Fault when attempting to print value in C
问题 The following C code returns a "segmentation fault" error. I do not understand why it does not return the value 20. What is my error? #include <stdio.h> int main() { int* n; *n = 20; printf("%i\n",*n); return 0; } 回答1: You haven't allocated memory to n , so *n = 20; attempts to write unspecified memory. Try #include <stdlib.h> int *n = malloc(sizeof *n); /* use n */ free(n); 回答2: You haven't allocated space for your int , you've only declared a pointer to an int . The pointer is uninitialized