Welcome, Guest. Please login or register.

Author Topic: Some help with C syntax please  (Read 2082 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: Some help with C syntax please
« on: August 14, 2006, 10:12:03 AM »
@golem
It's typecasting (just 'casting' in short). It's '(type)' before the expression.

GetMsg() returns 'struct Message *', but here we want to assign the return value to 'struct IntuiMessage *' type variable. To avoid a warning about a type mismatch, we need to cast the return value so that it matches the type we assign it to.

Thus:
Code: [Select]
message = [b](struct IntuiMessage *)[/b] GetMsg(...)

Most of the time trying to assign a return value to different type variable is a programming error. When it is not (like for example here), you want to cast the type to tell the compiler you 'really mean it'.


Even more details (ignore this if you don't feel like it yet):

This type of casting is often used when the type returned in embedded inside some other structure. In this case, if you take a look at the struct IntuiMessage from intuition/intuition.h, you will see that:
Code: [Select]
struct IntuiMessage
{
        struct Message       ExecMessage;

        ULONG                Class;
        UWORD                Code;

        ...

};

As you can see the structure begins with 'struct Message', and this is what GetMsg() returns pointer to ('struct Message *'). Since we know that intuition sends IDCMP messages of this type to the window UserPort, and GetMsg() will fetch these messages, we can now cast the pointer to 'struct IntuiMessage *' to get access to the other fields.