Segmentation Fault when attempting to print value in C

后端 未结 3 1416
夕颜
夕颜 2020-12-07 01:47

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 

        
3条回答
  •  余生分开走
    2020-12-07 02:32

    You haven't allocated memory to n, so

    *n = 20;
    

    attempts to write unspecified memory.

    Try

    #include 
    
    int *n = malloc(sizeof *n);
    /* use n */
    free(n);
    

提交回复
热议问题