Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,124 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,778 people online right now. Registration is fast and FREE... Join Now!




strange problem with code

 
Reply to this topicStart new topic

strange problem with code

Pontus
5 Jun, 2007 - 09:53 AM
Post #1

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
here is my code to convert a string to a char array:
CODE
void stringtochar(string from,char* const to){
for(int a= 0;a<from.size();a++)
*(to+a)=from[a];
}

if i give hello as string my output is like this:
CODE
hellox$


This post has been edited by manhaeve5: 5 Jun, 2007 - 09:55 AM
User is offlineProfile CardPM
+Quote Post

Topher84
RE: Strange Problem With Code
5 Jun, 2007 - 10:40 AM
Post #2

D.I.C Head
Group Icon

Joined: 4 Jun, 2007
Posts: 232


Dream Kudos: 25
My Contributions
Why not type cast the string to array?
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Strange Problem With Code
5 Jun, 2007 - 10:56 AM
Post #3

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
and how does that work?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Strange Problem With Code
5 Jun, 2007 - 11:23 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
You could simply use the c_str() method of the string class.

http://www.cplusplus.com/reference/string/string/c_str.html

http://www.cppreference.com/cppstring/c_str.html
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Strange Problem With Code
5 Jun, 2007 - 11:27 AM
Post #5

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
but its just a test to understand pointer array's
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Strange Problem With Code
5 Jun, 2007 - 01:07 PM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
A c-string is zero terminated. This means that you MUST end all c-strings with a 0. This means that a char array must be at least 1 char longer than the length of the string.

CODE
void stringtochar(string from,char* const to){
    for(int a= 0;a<from.size();a++) { to[a]=from[a]; }
    to[from.size()]=0;
    return;
}

User is offlineProfile CardPM
+Quote Post

Pontus
RE: Strange Problem With Code
6 Jun, 2007 - 08:13 AM
Post #7

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
thanx, that worked
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:14PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month