• Advertisement

Archived

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

char* VS string

This topic is 5843 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
When working with null-terminated strings (text, in other words), either the STL 'string' or your own string class are good choices. But character arrays have their place. For one thing, it's better to pass them as function parameters than string objects, because there is less overhead. The function can then convert the array to a local string object.

~CGameProgrammer( );

Edited by - CGameProgrammer on February 23, 2002 11:32:57 PM

Share this post


Link to post
Share on other sites
quote:
Original post by bctorvik
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.



This is because your functions are not const-correct. Fix that... read any good C++ book on where and when to declare things const, and your problems will clear right up.

quote:
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.


This is BAD BAD BAD BAD BAD. There is a REASON c_str() returns a const value: it doesn''t want anything else changing it. If you do so (and since you''re throwing away the const, you might even not realize it... this is why const is in the language) really strange and hard-to-debug things will start happening, since the string takes up a different amount of space than the string object thinks it does.

Besides, one of the main reasons to use the STL is to make your code readable. your &at(0) idiom is not a construct most STL programmers would immediately understand, so if you can avoid it, you should.

If you REALLY need to use a const variable in a non-const context (and, in this situation, you definitely don''t), use const_cast.

Share this post


Link to post
Share on other sites
By all means, I wasn''t trying to state the way I presented as the IDEAL way of doing things, but it does work in as much as I''ve seen. An example of why I''ve used this is the following: you''re given a big library of ugly code that DOESN''T use good programming practices (i.e., using const, as you stated). You could go through the 10+k lines of code and change all the functions that are not declared const char*, or you could get around it the way I proposed. As long as lives don''t depend on your code, what I said should do you just fine

Of course you should program using good practices when you''re creating a program from the ground up; it was just an example

Share this post


Link to post
Share on other sites
Hey, if you''re picky, why not
#define string char*
And don''t waste time on integer to string, that what
itoa(,,) is for.

Share this post


Link to post
Share on other sites
std::string implementations also usually use reference counting, so copying long strings is instantaneous, and the string knows when its modified to create a real copy of its data.

char* never goes away because it''s useful for byte streams (although I use unsigned char* myself for byte streams).

Share this post


Link to post
Share on other sites
quote:
Original post by krez
perhaps you should face your fears and just learn how to use char*... before it is too late...


Too late for what exactly?

--

When horses lift their heads up high to look at something, they're looking far into the distance. To see things that are closer, they lower their heads.


Edited by - SabreMan on February 25, 2002 6:29:09 AM

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
Too late for what exactly?

why, too late to save yourself from the Dark Side of the Force, of course!

Share this post


Link to post
Share on other sites

  • Advertisement