Archived

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

johnnyBravo

how to convert char to char * ?

Recommended Posts

johnnyBravo    100
ive got: char * string; char string2[10]; and i want to make string = string2. but when i try things like strcpy eg... this is orginal text: "hi there matie s" and after the conversion i get...: "hi there matie sÇ ↕" is there any proper way of converting it?

Share this post


Link to post
Share on other sites
Alpha_ProgDes    6921
char * string = NULL;
char string2[10] = {0};

try that. it should work.

and that''s why in programming class they tell u to initialize your variables, especially your pointers/arrays.

then you don''t things like what you got

Share this post


Link to post
Share on other sites
Pipo DeClown    804
Maybe strdup() - or something like it - will work out.
It allocates memory for a string and returns the pointer to it.

char *string = 0;
char src[] = "MyString";

string = strdup( src ); // not sure if this would compile, but it should work.

delete[] string; // delete the stringmemory
string = 0;

--
You''re Welcome,
Rick Wong
- sitting in his chair doing the most time-consuming thing..

Share this post


Link to post
Share on other sites
OrangyTang    1298
quote:
Original post by johnnyBravo
c++


Then ditch those unsafe, error prone char* and go the proper C++ way: std::string. Using char* for strings in C++ is just plain stupid (or ignorant, take your pick ).

std::string can and will hide all these problems from you, and prevent you making any silly mistakes.

Share this post


Link to post
Share on other sites
quasar3d    814
quote:
Original post by johnnyBravo
ah that sucks about the char *

is it ok to use the lpstr? instead of the std::string?




no. lpstr is just typedef''d as char* so it doens''t make any difference at all. std::string does has everything you need, and if you want to pass it to a c stryle function or so, you can always get a c style string with s.c_str();

with c strings it is very easy to get buffer overflows, and we''ve seen with valve what can happen with that. If outlook had just used std::string, this wouldn''t have happened



My Site

Share this post


Link to post
Share on other sites
Pipo DeClown    804
quote:
Original post by SabreMan
quote:
Original post by Pipo DeClown
string = strdup( src );
delete[] string;

That should be free(string), strdup is a C function.



It doesn''t make any difference. At least, that''s what my compiler says. Maybe it''s not standard, though.

--
You''re Welcome,
Rick Wong
- sitting in his chair doing the most time-consuming thing..

Share this post


Link to post
Share on other sites
SabreMan    504
quote:
Original post by Pipo DeClown
It doesn''t make any difference.

It certainly does make a difference. You *must* match malloc() with free(), new with delete and new[] with delete[]. Any mismatch, such as malloc()-delete is declared as illegal by the C++ Standard (it engenders undefined behaviour).
quote:

At least, that''s what my compiler says.

You cannot and should not put all responsibility for program correctness with the compiler. In this particular case, the compiler is not required to diagnose the problem. The responsibility for matching malloc() with free() lies with the programmer.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:

Then ditch those unsafe, error prone char* and go the proper C++ way: std::string. Using char* for strings in C++ is just plain stupid (or ignorant, take your pick ).

std::string can and will hide all these problems from you, and prevent you making any silly mistakes.



I would probably say something about the same but I would rather use my own string class. Anyways the above is a stupid reply especially as the OP doesn''t know how C-strings work, which should be known by anyone that programs with c/c++. So the post I quoted is kind of eh uneccessary, as if the poster skips learning c/c++ strings he might have a nightmare later on.. eh...


(Oh and can someone tell me why they have some kind of very easy encryption thingy with the stl library? I mean if it''s not some kind of encryption they should really look over their coding style, or is it ment to make the code look more complex then it really is?)

Share this post


Link to post
Share on other sites
Leffe    301
quote:
Original post by Anonymous Poster
(Oh and can someone tell me why they have some kind of very easy encryption thingy with the stl library? I mean if it''s not some kind of encryption they should really look over their coding style, or is it ment to make the code look more complex then it really is?)


I assume you are talking about Microsoft''s STL... well... it''s a good place to lean obfuscation, ioccc.org is another nice place for learning.

And other STLs are not much nicer, you aren''t really supposed to look at it

Share this post


Link to post
Share on other sites
Arild Fines    968
quote:
Original post by Leffe
I assume you are talking about Microsoft''s STL... well... it''s a good place to lean obfuscation, ioccc.org is another nice place for learning.

And other STLs are not much nicer, you aren''t really supposed to look at it


According to Dinkum, it really isn''t obfuscated. Apparently it is Plauger''s real style(Didn''t he cowrite "Elements of programming style"?).

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites
Sneftel    1788
If you think that code is obfuscated, you should really learn to read code better. It''s nice and easy to follow, especially if you''re looking through the Standard at the same time.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
OrangyTang    1298
quote:
Original post by Anonymous Poster
I would probably say something about the same but I would rather use my own string class. Anyways the above is a stupid reply especially as the OP doesn''t know how C-strings work.


Yeah ok. You do your own thing and write your own String class. I''ll just stick with the stl version, which has been tried and tested by thousands of other programmers, and worked on for many more hours than you''ll put into your own string class.

Meanwhile you''ll spend time (hours? days? weeks?) writing - and over a greater period of time - debugging your own string. And after all that wasted time, will you really have something better?

The fact that the stl string hides annoying and error prone details about the implementation (such as null terminated char*) is a good thing, especially to someone new to a language. If they really want to they can come back and learn about the char* way later.

Share this post


Link to post
Share on other sites
sjelkjd    171
quote:
Original post by OrangyTang The fact that the stl string hides annoying and error prone details about the implementation (such as null terminated char*) is a good thing, especially to someone new to a language. If they really want to they can come back and learn about the char* way later.



Yep. In fact, it is because of the fact that c++ is *not* learned in this manner that people write poor c++ code, abundant with buffer overruns.

Knowing about char* before std::string is NOT a good thing.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by OrangyTang

Using char* for strings in C++ is just plain stupid (or ignorant, take your pick ).




or backwards compatible.

oh wait, was that not one of the options?

Share this post


Link to post
Share on other sites