char* = char[]

Started by
23 comments, last by Staffan E 19 years, 3 months ago
Quote:Original post by Zahlman
Quote:Original post by Anonymous Poster
You need to add "meh = &blah[0];" before the sprintf.

It should work perfectly without any other changes.


Of course, it will still end up outputting the desired text and will therefore "work perfectly"


QED.
Advertisement
Quote:Original post by Zahlman
The original code has bugs, which Xai and many others pointed out, and the suggested inclusion cannot prevent those problems.

The statement "meh = &blah[0];" (which is equivalent to "meh = blah;") does not do what the OP wants - it will make meh point at the array data, when the OP clearly wants meh to point at a separate copy of that data.

"adding it before the sprintf" is just silly, as a result - why make a pointer point at an array and then sprintf the array contents to the location indicated by the pointer? The sprintf becomes a no-op. Of course, it will still end up outputting the desired text and will therefore "work perfectly" - except that it won't, because the previous assignment of the string literal to the char array is still wrong!

I made an attempt to demonstrate that I know whereof I speak. You are hereby challenged to follow suit.


There is no need to challenge, because I just misunderstood. But the way you replied at first is easy to misunderstand, you could've said that whole thing earlier. Anyway, I thought you were saying the syntax is wrong and bad, and did not know you were talking about the result not being the OPs wanted result.

Cya!

Quote:Original post by Zahlman

All of which (and more) is why rational, sane people use std::string in C++.


Then I really must go check with my local mental institution. I've never thought myself as very irrational or insane, and neither have people around me to my knowledge. Still I do use char arrays instead of std::string since I'm fluent with them.

/Staffan
Hack my projects! Oh Yeah! Use an SVN client to check them out.BlockStacker
There are other explanations besides irrationality or insanity, of course [smile] Just don't come crying to me when you have to track down the bugs [wink]
Well I guess it eventually comes down to the fact that there is no harm in knowing many ways to do the same thing. <- [Boxed wisdom]
Hack my projects! Oh Yeah! Use an SVN client to check them out.BlockStacker

This topic is closed to new replies.

Advertisement