2

I'm currently learning C programming. This is the C code to encrypt a string.

#include <stdio.h>
#include <stdlib.h>

void encrypt(char *message){           // Here message is a character pointer - array
    //char c;
    while(*message){
        *message = *message ^31;
        message++;
    }
}

int main(int argc, char const *argv[])
{
    char *msg = {"Hello Howdy?"};
    encrypt(msg);
    return 0;
}

I'm getting 'Segmentation fault(core dumped)' as the output. It compiles though.

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
  • 3
    You can't change a string literal. Use `char msg[]` and it'll be stored as a character array which you'll be able to manipulate. – iBug Dec 26 '20 at 09:53
  • @iBug doesn't , char *msg same as char msg[0] ? – technextgen Dec 26 '20 at 09:56
  • 1
    Literal strings in C are not modifiable, they are in essence *read only*. You need to create your own array (as suggested by @iBug) like `char msg[] = "Hello Howdy?";`. – Some programmer dude Dec 26 '20 at 10:00

0 Answers0