Advertisement

Santa are u there .....

Started by December 24, 2000 04:43 PM
12 comments, last by RealityMonster 23 years, 10 months ago
Well i used to do C style strings. But just finished a 2 month coding session in Java. So I need a String that can do string.whatNot() rather than character arrays n char *''s;
Whats a better solution? I would like growable strings.
How do you guys store your strings in win32?

s.c_str() was what I was looking for but its return type is
const char *. creatGLWindow requires char * and casting is not possible. When i do

title = (char *) malloc(sizeof(s));

the output is santaIIIIIIIIIIIIIIyyyy all with dots on top

?ideas?
Easy, just dynamically allocate the storage.

BTW, malloc doesn''t clear the array, so zap it with memset. In your case it seems the string is NOT null terminated, so that is why your getting crap.

(p.s, malloc? is old... use new! )

p.p.s, why not use a debugger, and set a breakpoint right above the call, so you can see the values in the string, my guess is they are all above normal ASCII char set. Which is also why you should really use memset, since your too lazy to add a \0 at the end of the sting you want. :D



Advertisement
I use:

char* str;
str = "This is a non-array string";
//Then you can go:
str[6] = 'n';
printf("str = %s", str);

Yeah I know printf is old but it's easy. The code will output:
"This in a non-array string"
Notice the changed 's' to 'n'

Or you can just use LPSTR



Open mouth, insert foot

Edited by - oglman on December 27, 2000 11:18:01 AM
quote: Original post by oglman

I use:
char* str;str = "This is a non-array string";//Then you can go:str[6] = ''n'';printf("str = %s", str); 



you are corrupting memory doing this - although you probably won''t see the effects, if you added this line:
printf( "This is a non-array string" ); 

... you will probably get the n again. str is pointing to that string in memory, the assignment operator is not making a copy.
Use strdup or new char[strlen(str)+1] instead.

~~~
Cheers!
Brett Porter
PortaLib3D : A portable 3D game/demo libary for OpenGL
~~~Cheers!Brett PorterPortaLib3D : A portable 3D game/demo libary for OpenGLCommunity Service Announcement: Read How to ask questions the smart way before posting!

This topic is closed to new replies.

Advertisement