C segmentation fault-char pointers

后端 未结 3 1975
孤城傲影
孤城傲影 2021-01-28 06:36

I need help figuring out why I am getting a segmentation fault here. I have gone over it and I think I am doing something wrong with the pointers, but I can figure out what.

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-28 06:52

    That pointer a does not get the actual space where to store the data. You just declare the pointer, but pointing to where? You can assign memory this way:

    char *a = malloc(1);
    

    Then it won't segfault. You have to free the variable afterwards:

    free(a);
    

    But in this case, even better,

    char a = 'a';
    encript(&a);
    

提交回复
热议问题