Archived

This topic is now archived and is closed to further replies.

Fusi0n

chars and pointers... grr!

Recommended Posts

hi all, the answer to the question im about to ask will probably be a little obvious to most of you, its got something todo with chars :s i was brought up using borland c++ builder (:s i know) which had a very easy way of managing strings (kinda like stl) so i never had much experience with chars because bcb's inbuilt string did everything i needed. now im using vc++ and dont have the option of using these strings, not due to availability, i could use stl - but because the code i have to write is focused on speed this isnt viable, so ive gotta use raw char *. ive experimented with them and to a certain extent acheived what i set out to do. however i have come seriously unstuck on a problem that i can see no way of solving - its post on gamedev time! right... basically im writing a console frontend for a game engine im writing. ive got the input, storage and the like all coded. i store commands recieved in a buffer which is executed every frame and the output is spat out to the console. the command buffer and console text are both stored using linked lists of classes that represent one line of text: //--------------------------------------------------------------- class CCommandBufferLine{ public: //------------------------------------- CCommandBufferLine(char *ps_text){ } //------------------------------------- ~CCommandBufferLine(){ if(p_next) p_next->p_prev = p_prev; if(p_prev) p_prev->p_next = p_next; p_next = p_prev = NULL; } //------------------------------------- CCommandBufferLine *p_next; CCommandBufferLine *p_prev; //------------------------------------- char s_text[con_MAX_INPUT_LINE_SIZE]; //------------------------------------- }; //--------------------------------------------------------------- the class that stores the text for the console is similar. now my problem is actually how to get the text stored in the passed character string into s_text. i dont have a clue, ive tried everything i can think of, its definatly summut todo with pointers, as before classes of this type are instantiated, that char *ps_text thats passed in is passed around a few functions before it gets passed to the creation of the console line / buffer. heres a brief overview: RecieveInput(){ AppendToBuffer(s_input_line); } AppendToBuffer(char *ps_text){ CCommandBufferLine *p_temp_command_line = new CCommandBufferLine(ps_text); } by the way s_input_line is defined as char s_input_line[con_MAX_INPUT_LINE_SIZE]; i was wondering if anyone can see what im doin wrong - i traced through the program and it seems that it all goes a bit pete tong when i try and pass the string to other functions :< if anyone can shed some light on this st00pid problem id be a very very happy person :D thanks, Fusi0n^ "We are what we repeatedly do. Excellence, then, is not an act, but a habit." - Aristotle [edited by - Fusi0n on December 24, 2002 7:40:27 PM]

Share this post


Link to post
Share on other sites
I don't really understand what you mean. What error do you get? What exactly happens at the point it goes 'pete tong'?
Do you want the functions like strcpy, crtcat, etc? (copy a string [a = b], and append a string [a += b])

Edit: If i were you, i'd just stick to std::string though. Although thats just an oppinion (please no "Never! C-strings forever!"s )

Edit 2: You're probably just assigning the pointer (ps_text = s_text or something). That would cause the contents of the buffer to get messed up at seemingly unexpected points. Check your code for this, or switch to std::string.



[edited by - Evil Bill on December 24, 2002 7:48:53 PM]

[edited by - Evil Bill on December 24, 2002 7:57:01 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Fusi0n
i could use stl - but because the code i have to write is focused on speed this isnt viable, so ive gotta use raw char *.



This question is trotted out as often as your statement about the stl''s speed so... well... how do you know std::string is too slow for you? Have you done extensive testing in release mode builds and compared the results to using C strings? My guess is no.

Anyway, the method you''re looking for is strcpy. First argument is the C string to copy to, second is the source string. It copies all the data from the source string until the first null character into the destination string.

Share this post


Link to post
Share on other sites
lo, ur quite right, i cant just dismiss std:string, but i like to roll my own if i can

i think im doing something wrong when i pass the strings to functions. strcpy - can the source and dest be an array or do they have to be pointers, and if they do do they need to be pointing at a string that already exists when calling the function?

thanks, fusi0n

Share this post


Link to post
Share on other sites
lo, ur quite right, i cant just dismiss std:string, but i like to roll my own if i can

i think im doing something wrong when i pass the strings to functions. strcpy - can the source and dest be an array or do they have to be pointers, and if they do do they need to be pointing at a string that already exists when calling the function?

thanks, fusi0n

"We are what we repeatedly do. Excellence, then, is not an act, but a habit." - Aristotle

Share this post


Link to post
Share on other sites