Archived

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

Mike-21

A probably simple pointer questions...

Recommended Posts

Mike-21    122
can anyone tell me why this will work? unsigned char* a; double b= 45456464645.43564564564; a= reinterpret_cast(&b); double c= reinterpret_cast(*a); so c at the end equal the exact value of b so this IS okay.. but why this time this can''t be done? unsigned char* a; char b[50]= "0123456789012345678901234567890123456789012345678"; a= reinterpret_cast(&b); std::string c= reinterpret_cast(*a); it do a 1st chance exception when a put value of a to c..? by the way i''m using Visual Studio 6... i''ve tried the same code in Borland C++ 5 and... well it seem to work can anyone explain me the problem??!?

Share this post


Link to post
Share on other sites
Sneftel    1788
A reinterpret_cast will do really odd things when trying to treat a character array as a std::string. Do you even know what reinterpret_cast does??


Don't listen to me. I've had too much coffee.

[edited by - sneftel on October 22, 2002 3:43:18 PM]

Share this post


Link to post
Share on other sites
Mike-21    122
well i''wasn''t knowing there was danger doing this with char array & string... and how would you do this then? string or array?

and by the way Sneftel i know that reinterpret_cast is used to convert a pointer to another one by doing NO test if it''s possible, it simply do it, so doing this, you can cause a lot of problem if not used correctly..., this is what i know about it, fell free to correct me or add any comments on what it do

Share this post


Link to post
Share on other sites
Sneftel    1788
Right. And treating a character array as a string is one of those things that causes problems.

If it worked well before, I would assume that Borland''s STL puts a pointer to the string as its first element (i.e. you got lucky). VC++ uses Dinkumware''s STL, which apparently does not.

You don''t need it anyway. Use the c_str() member function to get a pointer to a char array from a string.


Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites