2
// ExampleCodes.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdio.h>
#include<iostream>
using namespace std;    

char* stringReverse(char* s)    
{    

    char temp, *p,*q;    
    q = s;    
    while( *(++q));    
    for( p = s; p &lt; --q; p++)    
    {    
        temp = *p;    
        *p = *q;     
        *q = temp;    
    }    
    return s;    
}    


int _tmain(int argc, _TCHAR* argv[])    
{    

    stringReverse("StringReverse");    
    return 0;    
}    
Starkey
  • 9,673
  • 6
  • 31
  • 51
  • Indent your source code with four spaces to get it to display correctly. – Jack Kelly Sep 12 '10 at 23:31
  • Almost exactly the same as http://stackoverflow.com/questions/480555/modifying-c-string-constants , http://stackoverflow.com/questions/1614723/why-is-this-c-code-causing-a-segmentation-fault , http://stackoverflow.com/questions/2124600/how-to-reverse-a-string-in-place-in-c-using-pointers and many more... – Mark Byers Sep 12 '10 at 23:34
  • 2
    Shouldn't the compiler atleast give you a warning about passing a const char* to a char* parameter. – Alexander Rafferty Sep 12 '10 at 23:38
  • 4
    You seem to have accidentally omitted your question. – johnsyweb Sep 13 '10 at 00:08
  • @Alexander Rafferty: The type of string literals in C is `char []`, which decays to `char *` (this despite them being unmodifiable). This is because string literals pre-date the `const` keyword. – caf Sep 13 '10 at 00:12
  • @caf: Actually this is somewhat wrong. A string literal is of the type `const char[]`. But, yes, there's a (deprecated) conversion from a string literal to `char*`, to allow old code to compile. – sbi Sep 13 '10 at 06:35
  • I suppose this is missing a `homework` tag? Because otherwise the answer would be "use `std::reverse()`!" – sbi Sep 13 '10 at 06:36
  • @sbi: The C standard just says the elements of the array that make up a character string literal have type `char`. There is no mention of `const`. I am led to believe that this is different in C++, however. – caf Sep 13 '10 at 07:08
  • @sbi: In C (when we started the discussion, this question was tagged `C`), a string literal is explicitly specified as being an lvalue (which implies you can legally take its address with `&`). It's an array type with elements of type `char`, which gives it type `char [N]` where N is the length including nul terminator. This means it decays to type `char *` when used in most expressions. – caf Sep 13 '10 at 21:50
  • @caf: I didn't know this was tagged `C`. When I came this already was a `C++` question. Anyway, I already said I'm quite messed up on the issue, so you might be right for C++, too. – sbi Sep 14 '10 at 05:17

5 Answers5

8

You can't modify constant string literals.

stringReverse("StringReverse");

You could use a character array instead:

char str[] = "StringReverse";
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
5

String literals, like "StringReverse", are not allowed to be modified in C. Use an array:

char str[] = "StringReverse";
stringReverse(str);

Note that your stringReverse() function has undefined behaviour if you feed it a zero-length string.

caf
  • 233,326
  • 40
  • 323
  • 462
0

You never allocated any memory for p. You can try p = new char[sizeof(s)];

Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
Alexander Rafferty
  • 6,134
  • 4
  • 33
  • 55
0

Perhaps you should write something like this:

// in - string to reverse
// out - buffer for reversed string
// l - size of in

StringReverse(char* in, char* out, int l) {
  for (int i=0;i<l;i++) {
    out[l-(i+1)] = in[i];
  }
}

*There is very very little to no speed difference between [] and *(p++) / *(p--)

Alexander Rafferty
  • 6,134
  • 4
  • 33
  • 55
-1

Well I was going to clean up the awful source in the OP but instead I'll just post a cleaner version:

#include <stdio.h>
#include <string.h>

void reverse_string(char *s)
{
    char *q = s + strlen(s);
    for (char *p = s; p < --q; p++)
    {
        char temp = *p;
        *p = *q;
        *q = temp;
    }
}

int main()
{
    char s[] = "StringReverse";
    reverse_string(s);
    puts(s);
    return 0;
}

I hope for your sake Amit that you're still a student.

Update0

This is actually pure C, so learn from and awe at its performance but don't start writing C++ like this.

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526