Thread: structure pointer

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Question structure pointer

    I have the following structure RGB:

    struct RGB{__int8 B, G, R;} *pLcd;

    Using a pointer (*pPanel) I call the function GetBitmapBits().

    pLcd = (RGB *)(*pPanel)->GetBitmapBits();

    What does (RGB *) do?

  2. #2
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    (RGB *) casts the result of (*pPanel)->GetBitmapBits() to (RGB *)

    so, pLcd stores a RGB Pointer.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    9

    simple

    pLcd is a variable of struct RGB.
    infront of whatever value you will put
    (rgb *) it would convert that value to rgb type.
    like int num;
    (rgb *)num;
    num was earlier an integer now it is pointer of rgb type.

    i am just a student so dont mind if i am wrong
    i myself need the help of your esteemed self.

  4. #4
    *
    Guest
    * <- that is _not_ a "pointer"

    That is an asterisk. It tells the compiler you want it to work with the _address_ of the specified variable of the specified type, not the _contents_ of the variable.

    when you say

    Code:
    typedef struct
       {
       unsigned char  red;
       unsigned char  blue;
       unsigned char  green;
       }RGB;
    you are creating a variable _type_. you can create any number of variables of type 'RGB'.

    When you pass a variable of this type to another function, or procedure, it's faster and more effecient to pass it's _address_ (that is, where it resides in RAM), so that the function can manipulate the actual data indirectly.

    And here is the specific difference:

    Code:
    void  foo1(RGB);                                   /* prototypes */
    void  foo2(RGB*);
    int    main(void);
    
    int main(void)
       {
       RGB myRGBStruct;
    
       myRGBStruct.red = 0xFF;
       myRGBStruct.blue = 0xCC;
       myRGBStruct.green = 0x00;
    
       foo1(myRGBStruct);                         /* pass a copy of myRGBStruct to the func */
       foo2(&myRGBStruct)                        /* pass the addressof the original myRGBStruct to the func */
    
       return(0);
       }
    
    void foo1(RGB myRGB)
       {
       myRGB.blue = 0xEE;
       }                                                       /* <- myRGB is _gone_ off th stack when we return */
                                                                /* from this procedure, so 'myRGBStruct' in main */
                                                                /* was not affected. */
    
    void foo2(RGB *myRGB)
       {
       myRGB->red = 0xAA;                        /* myRGBStruct.red in main(), just got changed */
       }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. method returning a pointer to a structure
    By nacho4d in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2009, 10:01 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. how to cast a char *mystring to a structure pointer ??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 08:59 AM
  4. Pointer to a structure
    By frenchfry164 in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 06:35 PM
  5. Pointer to structure problem
    By unregistered in forum C Programming
    Replies: 3
    Last Post: 12-24-2001, 07:54 AM