Thread: Pointer to Structure Error Message

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    12

    Pointer to Structure Error Message

    Hello All,
    I am working through the example problem at Prime Pattern | CodeChef. (As a side note: This is a very good site for anyone that wants to practice their programming skills.) I have made a structure and pointer to it. I am getting a compiler error that I don't understand. Here is my structure and pointer:

    Code:
    typedef struct
    {
       int8_t x_coor;
       int8_t y_coor;
       uint8_t square_count;
       uint8_t move_count;
    } movement;
    
    typedef movement *pt_to_movement;
    Here is where I defined one of each and tried to assign the address of the structure to the pointer.

    Code:
    void moving_init(void)
    {
       movement mover;
       pt_to_movement pt_to_mover;
       pt_to_mover &mover;
       mover.x_coor = 0;
       mover.y_coor = 0;
       mover.square_count = 0;
       mover.move_count = 0;
    }
    The error says: invalid operands to binary & (have 'pt_to_movement' and 'movement'). It is pointing to the line: pt_to_mover &mover;

    I'm sure I'm missing something simple. Anyone see the problem?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Don't do this:
    Code:
    typedef movement *pt_to_movement;
    It is clear that movement* is the type "pointer to movement", so a typedef does not help. Rather:
    Code:
    movement mover;
    movement *pt_to_mover = &mover;
    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

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    12
    Quote Originally Posted by laserlight View Post
    Don't do this:
    Code:
    typedef movement *pt_to_movement;
    It is clear that movement* is the type "pointer to movement", so a typedef does not help. Rather:
    Code:
    movement mover;
    movement *pt_to_mover = &mover;
    This worked. Thanks. I'm still unsure why mine didn't work though. In this case, making a typedef of the pointer was trivial. But, in a larger program it might be useful. Do you know why I got the error message that I did? Thanks.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by johndeaton
    I'm still unsure why mine didn't work though.
    Look at this line:
    Code:
    pt_to_mover &mover;
    Allow me to insert a space:
    Code:
    pt_to_mover & mover;
    What you are doing is trying to take the bitwise and operation between pt_to_mover and mover, then discard the result. However, these objects don't allow for bitwise and, thus the compile error.

    Quote Originally Posted by johndeaton
    In this case, making a typedef of the pointer was trivial. But, in a larger program it might be useful.
    That is true. It would be useful if you were creating a typedef for an opaque pointer type.
    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

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    12
    Thanks for your comments.
    John

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2011, 10:40 PM
  2. How to pass pointer to a structure in a message queue
    By smitsaboo in forum C Programming
    Replies: 4
    Last Post: 08-15-2009, 11:33 PM
  3. Pointer to Structure error explanation
    By hellogamesmaste in forum C Programming
    Replies: 5
    Last Post: 08-15-2009, 10:37 AM
  4. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  5. Message box / Structure question?
    By dxfoo in forum Windows Programming
    Replies: 5
    Last Post: 10-24-2005, 01:43 PM