Help thanks
Help thanks
Please don't put your whole message in a title line... some of us (me included) don't always look too closely at titles.
cptr is a pointer to a character or character array.
cptr is simply a pointer to a character, there is no array.
Being able to say cptr[0] instead of *cptr (were it initialised to something other than NULL), when trying to use what it points to does not make it an array.
Arrays and Pointers
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
It's not whether YOU know it, it's whether the clueless noob you're replying to can see through your mis-information or not.
Jeez, do you have to be so much of a ........ing drama queen every time someone dares to clarify or correct anything you post.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I read it as a reply to clarify to Dom, not you.Originally Posted by CommonTater
No, pointers of type char* are used to point to the first (or other) element of arrays of char all the time. Colloquially, we say that such a pointer points to an array even though it does not, but I can understand the desire to be accurate when explaining to a newbie.Originally Posted by CommonTater
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
So what's this a "picky pile on"?
And from another moderator to boot.
I said nothing wrong. I gave the OP a simple answer in terms he should have been able to understand.
WHY this? Huh? Why these last few days I've got Salem knit picking away at me over the stupidest stuff imaginable.
And why you? What possible dog do you have in this fight?
Here's a quarter... go call someone who cares!
Your post #2 is factually inaccurate. But that's fine, because Salem's post #3 clarifies without any kind of personal attack on you.Originally Posted by CommonTater
So, what pile on? What fight?
Here's your quarter... go call someone who cares!
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The inaccuracy is in what the pointer points to. Consider:Originally Posted by CommonTater
Colloquially, we say that p1 points to s or that p1 points to an array of char. However, in fact p1 points to the first element of s, i.e., p1 is a pointer to a char. p2, on the other hand, points to s, i.e., p2 is a pointer to an array of 6 char.Code:char s[] = "hello"; char *p1 = s; char (*p2)[sizeof(s)] = &s;
This is no problem for us since we understand what we really mean, but a newbie may not understand that there is a difference that becomes important when say, multi-dimensional arrays are involved.
Last edited by laserlight; 12-15-2011 at 12:13 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
After all this, he'll probably never come back.
In short, there is absolutely no way whatsoever to distinguish between a char that's on its own in memory and a char that's part of a 1-element array. They are exactly the same thing. So char * could easily be a pointer to either or both. I don't see there's anything to argue about there.
An array of length one is just the same as a single item itself! There's no "standard" terminology I'm aware of that pointing to a char automatically makes it be called an array or that pointing to a char array of length 1 makes it called just a char.
Personally, it's a pointer to a char. If I then decide I need more char's I'll turn it into an array. So for me, char * changes "name" depending on my intention of its use and the number of elements I choose to store in it.
- Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
- A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
- The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.