C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 24th, 2012, 11:45 AM
twigg twigg is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 twigg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 16 sec
Reputation Power: 0
Issues with a char pointer function

Code:
char* reverseName(char* name)
{
             int length = strlen(name);
	int pos = strlen(name)-1;
	char *tempArray = new char[length];
	for(int i =0; i<length; i++)
	{
		tempArray[i] = name[pos];
		pos--;
	}
	return tempArray;
}


This code returns:
Your name is optimus
Your name backwards is sumitpo께께슬슬슬슬■ε■ε■

where "optimus" is the value entered in the parameter

I'm pretty confused as to what's going wrong, and could really use some help. Thanks

Reply With Quote
  #2  
Old October 24th, 2012, 01:32 PM
ediz ediz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 102 ediz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 35 m 33 sec
Reputation Power: 3
To get rid of them, don't forget put a \0 in the end.
\0 means its the end of the string.

Code:


#include <iostream>


using namespace std;

char* reverseName(char* name);


int main()
{
	char t[] = {'o','p','t','i','m','u','s','\0'};
	char *test;

	test = reverseName(t);

	cout<<test;

	return 0;
}

char* reverseName(char* name)
{
	int length = strlen(name);
	int pos = strlen(name)-1;
	char *tempArray = new char[length];
	for(int i =0; i<length; i++)
	{
		tempArray[i] = name[pos];
		pos--;

		if(pos == -1)
			tempArray[i+1] = '\0';
	}
	return tempArray;
}



Reply With Quote
  #3  
Old October 24th, 2012, 06:19 PM
twigg twigg is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 twigg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 16 sec
Reputation Power: 0
Thank you sir, you don't know how much time you saved me.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Issues with a char pointer function


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap