Skip to main content
gamedev.net
🔒 Locked

string or char*

Started by siddim 3 replies 500+ views
Original Post
siddim
siddim
hi, i am building my little CWriter class in opengl, i first code it with char*, mainly because i don''t know much about string.h. Should i use string or stick to char*, i think it would be easier to convert int and float into string. Again, i don''t know much about string, any recommandation? If you recommand string, did you have any good tutorial or info site? all the reference i have from school are crap.. thanks. Siddim.
uNiQue0815
uNiQue0815
string ? is there something like ''string'' in c++ ??

you should use


char* myString;

and before using it, allocate memory for it.
(i.e. myString = new char[100]; for 100 chars.)
(don''t know, if the syntax is 100% right... i always do it with
myString = (char*)malloc(100);, but that''s c, not c++... )

or...
if you know the length at declaration time, you can write

char myString[100];

then myString will already be allocated 100 bytes... (but notice, that these will be [0..99], not [1..100])

you can then use (or is it stringS.h ?) for string operations (see MSDN for further help...)
IndirectX
IndirectX
quote:
Original post by uNiQue0815
string ? is there something like 'string' in c++ ??


In fact there is. It's called std::string and it resides in the standard <string> header. You can find some stuff about it at SGI's web site. And you should generally use std::string instead of char *s, unless you're learning how to implement std::string.

[edited by - IndirectX on June 14, 2002 2:01:27 PM]
---visit #directxdev on afternet <- not just for directx, despite the name
Dirge
Dirge
As a side note, there are 2 types of String data types you can use:
"CString" is a windows class.
"string" is used by the standard template library (STL) and the standard c++ library.

Use them and when you want a char * out of them, use the respective get char * functions for them (for CString it''s object.GetBuffer(), and for string it''s object.c_str()).

Then agan you could just dynamically allocate all your strings, but memory gets messy if you have lots of strings. BTW, by that I mean:
char *pName = new char[32];
_snprintf( pName, 32, "Hello my name is Bob" );


"Love all, trust a few. Do wrong to none." - Shakespeare

Dirge - Aurelio Reis
www.CodeFortress.com
"Artificial Intelligence: the art of making computers that behave like the ones in movies."www.CodeFortress.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.