C segmentation fault-char pointers

后端 未结 3 1976
孤城傲影
孤城傲影 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:59

    The problem is here:

    char *a;
    *a = 'a'
    

    Since the variable "a" is not initialized, *a = 'a' is assigning to a random memory location.

    You could do something like this:

    char a[1];
    a[0] = 'a';
    encrypt(&a[0]);
    

    Or even just use a single character in your case:

    int main(){
      char a = 'a';
    
      encrypt(&a);
      printf("test:%c/n",a);
    
      return 0;
    };
    

提交回复
热议问题