Below is an exceedingly simple example. It compiles fine using gcc on Mac OS X (Snow Leopard). At runtime it outputs Bus error: 10. What’s happening here?
char* a = "abc";
a[0] = 'c';
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your code sets
ato 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:
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.