#### Archived

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

# chars and pointers... grr!

This topic is 5754 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 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 on other sites
Arrays are pointers, and yes, the memory needs to be allocated.

E.g.

  char a[50];char b[50];strcpy(a,"Hello ");strcpy(b,"World!");strcat(a,b);printf(a);

##### 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

1. 1
2. 2
3. 3
4. 4
Rutin
14
5. 5

• 10
• 11
• 9
• 14
• 10
• ### Forum Statistics

• Total Topics
632652
• Total Posts
3007661
• ### Who's Online (See full list)

There are no registered users currently online

×