Thread: char *cptr = 0......is cptr the name of variable with pointer to char? or is it cptr?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    char *cptr = 0......is cptr the name of variable with pointer to char? or is it cptr?

    Help thanks

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    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
    And you think I don't know this?

    char* is used to point to arrays all the time.

    Really... way too picky.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Salem View Post
    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
    In the K&R days you could NOT assume it was even a pointer to an character; I have no reason to believe that that has changed.

    The compiler just assumes it is a pointer to an character and does pointer arithmetic using the size of char.

    Tim S.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by stahta01 View Post
    The compiler just assumes it is a pointer to an character and does pointer arithmetic using the size of char.
    +1

    Code:
    int main(void)
    {
       char mychar = 'A';
       int myint = 12345;
       char *cptr = 0;
       cptr = &mychar;
       cptr = malloc(10 * sizeof(char));
       cptr = (char*)&myint;
       return 0;
    }
    etc etc

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    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.
    And I get this from a moderator?

    Not good.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    And you think I don't know this?
    I read it as a reply to clarify to Dom, not you.

    Quote Originally Posted by CommonTater
    char* is used to point to arrays all the time.

    Really... way too picky.
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    I read it as a reply to clarify to Dom, not you.


    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.
    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!

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    I said nothing wrong. I gave the OP a simple answer in terms he should have been able to understand.
    Your post #2 is factually inaccurate. But that's fine, because Salem's post #3 clarifies without any kind of personal attack on you.

    So, what pile on? What fight?

    Here's your quarter... go call someone who cares!
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Your post #2 is factually inaccurate. But that's fine, because Salem's post #3 clarifies without any kind of personal attack on you.

    So, what pile on? What fight?

    Here's your quarter... go call someone who cares!
    How exactly is that factually inaccurate? That is what we use pointers for, is it not?.... pointing at stuff!

    Really, this is knit picking taken to the level of pathology.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    How exactly is that factually inaccurate? That is what we use pointers for, is it not?.... pointing at stuff!
    The inaccuracy is in what the pointer points to. Consider:
    Code:
    char s[] = "hello";
    char *p1 = s;
    char (*p2)[sizeof(s)] = &s;
    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.

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    After all this, he'll probably never come back.

  15. #15
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined reference to `RunSwmmDll(char*, char*, char*)'
    By amkabaasha in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2011, 12:33 PM
  2. Replies: 7
    Last Post: 09-19-2011, 01:37 PM
  3. Copy char array to char pointer
    By Suseela in forum C Programming
    Replies: 9
    Last Post: 08-06-2009, 12:49 PM
  4. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM
  5. Char-array vs Char-pointer
    By ripper079 in forum C++ Programming
    Replies: 12
    Last Post: 09-09-2002, 01:16 AM