Skip to main content
gamedev.net
🔒 Locked

Strings and Such

Started by DanTheRocker Oct 6, 2001 at 10:36 PM 12 replies 350+ views
Original Post
DanTheRocker
DanTheRocker
Is there any algorithm to convert a char str[255] to a char *str? And Vice versa? If so please, do tell.
-Dan
Oluseyi
Oluseyi
Actually, a char[255] is a char*, but the reverse isn''t true - yet. To make a char* a char[n] where n is a constant integer, you need to allocate a region of memory using one of the alloc functions (in C) or new in C++. When you''re done you''ll need to deallocate that memory using free in C or delete in C++.
DanTheRocker
DanTheRocker
Ok, *cough*. How exactly is that done?
-Dan
DanTheRocker
DanTheRocker
I''m not using C, im using C++. Can I get an example of how to use "new" and "delete" to make a char *str a char[n]? I''d be a huge help.
-Dan
Oluseyi
Oluseyi

char* c;
int n = 10;
c = new char[n];
// c is now char c[10];
// done with c?
delete c;

Since this is basic C++, I''d ask you to go read up on the language. This forum is not a tutorial ground to provide all solutions; it is to point you in the right direction to find solutions for yourself.
DanTheRocker
DanTheRocker
Thank ya. And yes, this post probably should have been on another board, but thanks for answering anyway.
-Dan
Oluseyi
Oluseyi
No problem.

I''m not saying don''t post questions like this here; I''m saying that we''re here to think through the problem with you. Please post, because that''s what makes these forums worthwhile - the prospect of learning from the questions of others (sorta like a classroom). I''m just saying that once you''ve been pointed in the right direction, you owe it to the forum - the "class" - to do some homework yourself and then come back with your results. That way, everybody learns faster.

Anyway, I''m glad your problem is solved and hopefully you''ve also gained a greater understanding of C/C++.
Arild Fines
Arild Fines
quote:
Original post by jesterlecodeur
Using char* is faster than std::string depending what you do.


Geezes...everyone is in such a hurry these days.



"A society without religion is like a crazed psychopath without a loaded .45"
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
TerranFury
TerranFury
I''m definately not a fan of the STL.....

Topic Locked

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

Sign in to reply to this topic.