-1

I am getting a seg fault for the below code, is there something wrong? Here I am trying to shift the bits in a. Also, I know char * is a read only memory.

So, we have to copy into char a[] and then modify it???

    char *str = "abc";

    *str = *str << 1; 
ameyCU
  • 16,489
  • 2
  • 26
  • 41
Invictus
  • 2,653
  • 8
  • 31
  • 50

2 Answers2

2

char* string literal is pointing to read-only memory. You need to use a char array:

char str[] = "Hello";
*str = *str << 1;

See: What is the difference between char s[] and char *s?

Community
  • 1
  • 1
Isaac Turner
  • 2,673
  • 1
  • 26
  • 25
0

What are you trying to do exactly ?

<< is a bitwise operator and *str will not refer to the whole string but only to the first character.

I also recommend you to put parenthesis around your pointer when doing any operations on it to be sure that the bit shift is not done on the address instead of on what is pointed at this address.

Tim
  • 1,853
  • 2
  • 24
  • 36