9 Replies - 1080 Views - Last Post: 30 March 2012 - 08:03 PM Rate Topic: -----

#1 turtleC++  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 118
  • Joined: 07-May 08

Reverse string with passing in char pointer and char array

Posted 28 March 2012 - 07:00 PM

Hi,

I try to reverse string but the function ReverseString() does not work using the second call. Why is that?

void ReverseString(char *c)
{
	int length = strlen(c);

	char *front = c;
	char *end = c + length - 1;
	char ftemp = NULL;
	char etemp = NULL;
	
	while (end >= front)
	{
		ftemp = *front;
		etemp = *end;
		*front = etemp;
		*end = ftemp;
		front++;
		end--;

	}

	cout<<c;

}

int _tmain(int argc, _TCHAR* argv[])
{
	char sentence[] = "hello world";

	ReverseString(sentence, reverseStr2);//first call and this works
	ReverseString("hello world", reverseStr2);//second call, but does not work

	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Reverse string with passing in char pointer and char array

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 3051
  • View blog
  • Posts: 9,291
  • Joined: 25-December 09

Re: Reverse string with passing in char pointer and char array

Posted 28 March 2012 - 08:20 PM

Please provide a complete compilable program that illustrates your problem, and indicate whether this is a C or C++ program.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 turtleC++  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 118
  • Joined: 07-May 08

Re: Reverse string with passing in char pointer and char array

Posted 29 March 2012 - 10:56 AM

the program is for C++.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 3051
  • View blog
  • Posts: 9,291
  • Joined: 25-December 09

Re: Reverse string with passing in char pointer and char array

Posted 29 March 2012 - 10:57 AM

Post the smallest complete program that illustrates your problem.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Reverse string with passing in char pointer and char array

Posted 29 March 2012 - 12:23 PM

With the code you've given, neither of the two calls will work because you've defined ReverseString to take one argument, but you're calling it with two. Also reverseStr2 isn't defined or declared anywhere.

That said, your problem most likely is that you can't mutate string literals. That's undefined behavior (and will cause a segfault or equivalent on the majority of platforms).
Was This Post Helpful? 1
  • +
  • -

#6 turtleC++  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 118
  • Joined: 07-May 08

Re: Reverse string with passing in char pointer and char array

Posted 29 March 2012 - 06:12 PM

Opp sorry. It was meant to have one argument. So it will be like this.

01      void ReverseString(char *c)
02	{
03	    int length = strlen(c);
04	 
05	    char *front = c;
06	    char *end = c + length - 1;
07	    char ftemp = NULL;
08	    char etemp = NULL;
09	     
10	    while (end >= front)
11	    {
12	        ftemp = *front;
13	        etemp = *end;
14	        *front = etemp;
15	        *end = ftemp;
16	        front++;
17	        end--;
18	 
19	    }
20	 
21	    cout<<c;
22	 
23	}
24	 
25	int _tmain(int argc, _TCHAR* argv[])
26	{
27	    char sentence[] = "hello world";
28	 
29	    ReverseString(sentence);//first call and this works
30	    ReverseString("hello world");//second call, but does not work
31	 
32	    return 0;
33	}


This post has been edited by turtleC++: 29 March 2012 - 06:13 PM

Was This Post Helpful? 0
  • +
  • -

#7 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 255
  • View blog
  • Posts: 1,663
  • Joined: 20-September 08

Re: Reverse string with passing in char pointer and char array

Posted 30 March 2012 - 03:25 AM

As well as noting what Sepp2k said above, you may want to study up on 'read-only' memory.
Was This Post Helpful? 1
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Reverse string with passing in char pointer and char array

Posted 30 March 2012 - 06:38 AM

You can do this
ReverseString("hello world", reverseStr2);

if you rewrite your function to take 2 arguments. The second argument should be an empty array in which the function can store the result. Then it won't matter whether the original string is an array or a string literal.

This post has been edited by r.stiltskin: 30 March 2012 - 06:39 AM

Was This Post Helpful? 1
  • +
  • -

#9 turtleC++  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 118
  • Joined: 07-May 08

Re: Reverse string with passing in char pointer and char array

Posted 30 March 2012 - 05:42 PM

View PostDavid W, on 30 March 2012 - 04:25 AM, said:

As well as noting what Sepp2k said above, you may want to study up on 'read-only' memory.

thanks, I will do that.

View Postr.stiltskin, on 30 March 2012 - 07:38 AM, said:

You can do this
ReverseString("hello world", reverseStr2);

if you rewrite your function to take 2 arguments. The second argument should be an empty array in which the function can store the result. Then it won't matter whether the original string is an array or a string literal.

earlier it worked with second argument, but I took one out but since I didn't know about read-only like previous posts mentioned. So i will read on that. thanks.
Was This Post Helpful? 0
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 255
  • View blog
  • Posts: 1,663
  • Joined: 20-September 08

Re: Reverse string with passing in char pointer and char array

Posted 30 March 2012 - 08:03 PM

Hint:

You may want to contrast ...

char test1[] = "0123456789";

with ...

char* test2 ="abcdefghij";

where you would need to first copy test2 into a 'buffer' to be able to reverse that copied string in that buffer ...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1