Thread: Pointers, how i hate you

  1. #1
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76

    Pointers, how i hate you

    I tried doing this
    Code:
    Current->Next = New_Cur;
    Next is a pointer, of type node. New_Cur is a const pointer of type node. For some odd reason the compiler is hollering that it can not do this.
    Now question time; Is there an easy way of making the pointer equal to a const pointer?

    The exact error is:
    Code:
    C:\...\Programming\Comp_Engine\SYSTEM\Entity_Lists.cpp|20|error: invalid conversion from 'const comp_node*' to 'comp_node*' [-fpermissive]|

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The compiler seems to be complaining about using a pointer to a constant node (const node *), not a constant pointer to node (node * const).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yes it's possible to force it to accept this, but it is generally a mistake. Const wouldn't be very robust if you could just take some pointer-to-const, assign that to a pointer-to-non-const, and then just write away over that data without worries. Const is meant to stop that.

    Probably either Next should be "const comp_node*", or New_Cur should be just "comp_node*".

    And finally, make sure you learn the difference between
    Code:
    const comp_node *x;
    //and
    comp_node *const x;
    so that you can know for sure that you are using the one you intended to be using.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Oh! thanks iMalc. I had never realised that there were two types of const calls in pointers! Good to know, and thanks again!

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    There's also comp_node const * x, which is identical to const comp_node * x.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I hate pointers or Pointer Issues
    By bolivartech in forum C Programming
    Replies: 9
    Last Post: 11-14-2009, 11:48 AM
  2. Man, I hate having to do this...
    By Jaken Veina in forum Windows Programming
    Replies: 4
    Last Post: 07-05-2005, 12:23 AM
  3. I know you all must hate me but another ?
    By Thantos in forum Windows Programming
    Replies: 4
    Last Post: 08-15-2003, 01:09 PM