1

I am trying to reverse a string (but that's not the problem that I have). The problem is trying to change the value of the string array given a certain index. However, every time I try to change the value at the index, I get a bus error. Namely, Bus error: 10. I'm not sure what this means. Also, I tried str[0] = "a" but this also gives me a bus error. Any suggestions to fix this?

#include <iostream>
using namespace std; 

void reverse(char* str){
    str[0] = 'a'; 

}


int main(){

    char* str = "hello"; 
    reverse(str); 

}
aejhyun
  • 612
  • 1
  • 6
  • 19
  • 1
    The answer is the same as the answer to this one, just recently asked: http://stackoverflow.com/questions/31816473 but also http://stackoverflow.com/questions/3075049 – Moby Disk Aug 05 '15 at 23:27
  • String literals are constants and can not be modified. – Jonathan Potter Aug 05 '15 at 23:29
  • 2
    Also: The compiler should give you a warning that your code is not valid. See http://coliru.stacked-crooked.com/a/ef79aa4884d08883 – Moby Disk Aug 05 '15 at 23:31
  • `str[0] = "a";` should definitely not even compile. How did it give you a runtime error? – chris Aug 05 '15 at 23:37
  • You should also get errors for `str[0] = "a"`. If you don't then you really need to turn up your compiler's diagnostic level. – M.M Aug 05 '15 at 23:49
  • @MobyDisk it's nothing to do with the first one you linked – M.M Aug 05 '15 at 23:50
  • @chris common compilers give "warning" in their default mode for conversion from pointer to integer – M.M Aug 05 '15 at 23:50
  • @MattMcNabb, Both the Clang and GCC on Coliru give errors with no warning options. I don't recall seeing compilers give warnings for that except in C mode. – chris Aug 06 '15 at 00:06
  • Note the code above has `str[0] = 'a'`, not `"a"`. – Jonathan Potter Aug 06 '15 at 00:29
  • @MattMcNabb The similarity is that the first link shows code declaring a string literal without const. – Moby Disk Aug 06 '15 at 13:36
  • @MobyDisk That's where the similarity ends though, the main thing about this question is writing to the string literal, which doesn't happen in the other one – M.M Aug 06 '15 at 20:54

2 Answers2

6

Allocate your string as an array on the stack and not as a pointer into a possibly read-only segment of your program.

char str[] = "hello";
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
  • I have one question, void reverse(char* str){}, am I passing a pointer or is * acting as a reference? – aejhyun Aug 06 '15 at 00:05
  • In C++, arrays "decay" into pointers in certain contexts. Here, the pointer argument to `reverse` starts out as a pointer to the first element of the array in `main`. – Greg Bacon Aug 06 '15 at 00:13
  • But when I write cout << str << endl;, why does it print out "hello"? Shouldn't it print only the first character of the string since it points to the first element of the array? – aejhyun Aug 06 '15 at 00:32
  • The type of `str` in `reverse` is pointer to `char`, which is treated by `std::cout` as a string. In contrast, plain `char` is a single character. – Greg Bacon Aug 06 '15 at 00:40
2

First of all, this line should atleast give you a warning:

char* str = "hello";

you are converting a string constant to a pointer, which is not allowed. To fix your code, you should use, char str[] = "hello" in main().

When you pass this array in reverse(), it decays to char*, now the question which you asked in previous answer's comment.

But when I write cout << str << endl;, why does it print out "hello"? Shouldn't it print only the first character of the string since it points to the first element of the array?

It is because the << operator on std::cout is overloaded. If you give it a char* or const char*, it treats the operand as a pointer to (the first character of) a C-style string, and prints the contents of that string:

const char * str= "hello";
cout << str; // prints "hello"

If you give it a char value, it prints that value as a character:

cout << *str;   // prints "h"
cout << str[0]; // prints "h"
Nishant
  • 1,635
  • 1
  • 11
  • 24