0

Here is my code:

    void reverseStr(char *str)
{
    if (str == NULL) return;
    int i=0, j=strlen(str) -1;
    while(i<j)
    {
        char temp = str[j];  //i think this is the cause of the problem
        str[j] = str[i];
        str[i] = temp;
        i++;
        j--;
    }
}

So here is where it is called:

int main()
{   
    char *str = "Forest Gump";
    reverseStr(str);
    cout << str;
}

Here is my error:

/Applications/TextMate.app/Contents/SharedSupport/Bundles/C.tmbundle/Support/bin/bootstrap.sh: line 7: 1931 Bus error "$3".out

Any thoughts? Thanks in advance.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
nababa
  • 1,251
  • 3
  • 13
  • 20
  • 5
    Please post the code that calls `reverseStr` . – CB Bailey Aug 16 '10 at 20:28
  • Is the string null terminated? – Paul Tomblin Aug 16 '10 at 20:29
  • 2
    It looks like TextMate crashed. Why do you think *your* source code had anything to do with that? Have you tried your code with some other editor? Does your code compile with the command-line C compiler? What about via XCode? – Rob Kennedy Aug 16 '10 at 20:29
  • When you've posted the calling code we'll know whether this is a duplicate of http://stackoverflow.com/questions/2345584/reversing-a-string-in-c . – CB Bailey Aug 16 '10 at 20:30
  • just a style comment, I would use a for loop instead of a while loop. Also, I think your method will break for symmetric strings. And this should probably be tagged C and not C++ or else you should just use a std::string – Falmarri Aug 16 '10 at 20:30
  • It would do to mention that this algorithm and the one @Charles mentions will fail globalization tests. If you're interested in details, check out http://msmvps.com/blogs/jon_skeet/archive/2009/11/02/omg-ponies-aka-humanity-epic-fail.aspx. – kbrimington Aug 16 '10 at 20:32
  • For me it gives EXC_BAD_ACCESS. –  Aug 16 '10 at 20:33
  • 1
    @Rob Kennedy: Presumably he's using textmate as an IDE to kick off his built code. bootstap.sh? – CB Bailey Aug 16 '10 at 20:34
  • have you considered running the program in a debugger ? – Andre Holzner Aug 16 '10 at 20:41
  • When I ran this code my computer caught fire and then exploded my house :( – Brian R. Bondy Aug 16 '10 at 20:42
  • This is decidedly not C. C doesn't use `<<` to mean anything but left-shift. – nmichaels Aug 16 '10 at 20:45
  • @Brian R. Bondy: I hope your insurance covers nasal demons. – torak Aug 16 '10 at 20:47
  • My compiler (gcc 4.2) gives me a warning when I try to assign a literal to a non-const char*: "deprecated conversion from string constant to ‘char*’". As you obviously use C++ (cout), I wonder if your compiler didn't give you that warning or if you just ignored it? – Frank Osterfeld Aug 17 '10 at 06:31
  • Possible duplicate of [Reverse C-style String? - C++](https://stackoverflow.com/questions/2197412/reverse-c-style-string-c) – ivan_pozdeev Aug 18 '18 at 16:56

3 Answers3

8

Str pointes to a fixed string. You are modifying it in place. In other words, you trying to change the text literal. Try this:

char *str = strdup("Forest Gump"); 
reverseStr(str); 
cout << str; 
free(str);
James Curran
  • 101,701
  • 37
  • 181
  • 258
4

String literals are read only memory, you can't reverse them, nor modify them in any way without encountering undefined behavior.

Copy your string into a buffer first, then pass in the buffer. Or declare an array instead of a pointer and initialize that array with a string initializer.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
0

The error is in a bash/shell script not in your program. Would you please post the bash script also?

schoetbi
  • 12,009
  • 10
  • 54
  • 72