Jump to content
  • Advertisement

Archived

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

Seriema

char* VS string

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! When I learned C++ (knew C first) I discovered string from the standard library and said "bye bye!" to char*. Now when I''m learning DirectX I see char* all over the place! No one uses string. Why?.. I really hate char* }+TITANIUM+{

Share this post


Link to post
Share on other sites
Advertisement
Hi
I can´t answer your question, I just wanted to express my mutual hate towards chars . By the way, you wouldn´t happen to know the answer to my question in a previous post (Need help)? It has to do with chars . I hardly use them myself but I felt that writing the IntToString function would be good training using pointers. Anyway, hate char, hate char...

Share this post


Link to post
Share on other sites
The reason is simple. DirectX doesn''t just interface with C++, but also with C, Visual Basic, Delphi and what have you.

Another thing to keep in mind is that "char*" doesn''t necessarily point to string data, but can be used to any pointer (although in my opinion, one should use "void*" instead).

And finally, I know some people just can''t stand having to
#include <string>
everywhere.

Share this post


Link to post
Share on other sites
First of all, I believe you can access the char * in the string object in the STL. Second of all, that is how it is taught in tutorials.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
just use c_str() to get a char* from your string.

Share this post


Link to post
Share on other sites
quote:
Original post by Seriema
Hi!

When I learned C++ (knew C first) I discovered string from the standard library and said "bye bye!" to char*. Now when I''m learning DirectX I see char* all over the place! No one uses string. Why?.. I really hate char*

}+TITANIUM+{


It''s called "legacy syndrome".

--

The placement of a donkey''s eyes in its head enables it to see all four feet at all times.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
It''s called "legacy syndrome".

you''d like to believe that, wouldn''t you? perhaps you should scroll up and read Kippesoep''s post...
fear leads to anger, anger leads to hatred , hatred leads to suffering... this is the path to the dark side of the Force...
perhaps you should face your fears and just learn how to use char*... before it is too late...

Share this post


Link to post
Share on other sites
quote:
Original post by Kippesoep
...in my opinion, one should use "void*" instead



Type checking



"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin

Share this post


Link to post
Share on other sites
Type checking? Sure, if the type must be strict. But where''s the sense in this?

LONG mmioWrite (HMMIO hmmio, char *pch, LONG cch);

which is a Win32 function for writing to a chunked file. If I want to save anything but a char I have to cast everything. That should simply use void, like fwrite does. That''s what I originally said, if the pointer doesn''t just point to char, but to any kind of data, use void.

Share this post


Link to post
Share on other sites
As the Ap said, you can use c_str to convert stl strings to char*, but I''ve found that sometimes a function cannot take in the const char* that is returned from c_str. Now maybe there''s another, more visually pleasing, way to get around that, but I''ve found doing &stl_string.at(0) works like a char* without the const nuisance.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!