Why can't I edit a char in a char*?

Your code sets a to a pointer to "abc", which is literal data that can't be modified. The Bus error occurs when your code violates this restriction, and tries to modify the value.

try this instead:

char a[] = "abc";
a[0] = 'c';

That creates a char array (in your program's normal data space), and copies the contents of the string literal into your array. Now you should have no trouble making changes to it.


You are trying to modify a string constant. Use this instead:

char a[] = "abc";
a[0] = 'c';

This

char* a = "abc";

relies on a dangerous implicit conversion from const char[] (the type of a string literal) to char*. (In C++ this conversion has been deprecated for more than a decade. I don't know about C, though.)

A string literal must not be altered.

Tags:

C

Gcc

C Strings