Thread: Pointer Type Argument

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    32

    Pointer Type Argument

    I have used this exact code numerous times in the past weeks, and it has always worked flawlessly.

    Code:
    typedef struct _LLNode   
    {
        int x;
        int y;
        int z;
        int dx;
        int dy;
        int dz;
        struct _LLNode *next;
    } LLNode;
    
    typedef struct _List     
    {                       
        LLNode *head;       
    } List;
     
    void addToFront(List *list, int x, int y, int z, int dx, int dy, int dz);
    
    #define FOREVER 1
    int main() {
        List list = {NULL};
        addToFront(&list, 100, 60, 10, -1, 1, -1 );
        addToFront(&list, 200, 110, 5, -1, 1, 1);
        addToFront(&list, 30, 30, 1, 1, 1, 1);
        REG_DISPCTL = MODE4 | BG2_ENABLE;
        PALETTE[252] = BLACK;
        PALETTE[253] = BLUE;
        PALETTE[254] = YELLOW;
        PALETTE[255] = RED;
        while (FOREVER) {
            LLNode *hd;
            hd = list->head; // ERROR HERE
            for(int i = 0; i < 3; i++){
            hd->y = hd->y + hd->dy;
            hd->x = hd->x + hd->dx;
            hd->z = hd->z + hd->dz;
            if(hd->y < 0)
            {
                hd->y = 0;
                hd->dy = -hd->dy;
            }
            if(hd->y>159 - hd->z)
            {
                hd->y = 159 - hd->z;
                hd->dy = -hd->dy;
            }
            if(hd->x < 0)
            {
                hd->x = 0;
                hd->x = -hd->x;
            }
            if(hd->x>239 - hd->z)
            {
                hd->x = 239 - hd->z;
                hd->dx = -hd->dx;
            }
            if(hd->z < 0)
            {
                hd->z = 0;
                hd->dz = -hd->dz;
            }
            if(hd->z > 10)
            {
                hd->z = 10;
                hd->dz = -hd->dz;
            }
            hd = hd->next;
            }
    }
    }
    I am getting my error with "hd = list->head;"
    As you can see, I'm using this pointer to traverse through the linked list. Every iteration, it updates with hd=hd->next;

    I am getting an error however. My compiler is telling me:
    error: invalid type argument of `->'
    However, the code seems valid to me, as I have used this method before. Any idea why I am getting an invalid type error?

    ---
    In addition, my IDE is applying the appropriate colors to all items, as it is recognizing the links as valid.
    Last edited by Brandon Smith; 04-14-2012 at 04:07 PM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    It's because "list" isn't a pointer type, so replace "->" with "."

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    That is not all the actual code. For starters, for NULL is from <stddef.h> or <stdio.h>.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I have used this exact code numerous times in the past weeks, and it has always worked flawlessly.
    So you must have always been dynamically allocating the list head so -> would have worked. But memcpy's pointed out the problem.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    32
    I can't seem to spot the difference's in my code that was allowing -> to work before, and now the dot notation to work. Can you explain the difference?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To access the members of a structure, you use the dot operator. To access them via a pointer, you can use the arrow operator instead (you don't have to, but you can).
    Code:
    struct foo x, *p;
    p = &x;
    
    z = x.thing;
    z = (*p).thing;
    z = p->thing;
    Take your pick.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-06-2010, 03:18 AM
  2. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 03:23 PM
  3. passing argument from incompatible pointer type
    By bhdavis1978 in forum C Programming
    Replies: 5
    Last Post: 03-17-2010, 12:42 PM
  4. passing argument from incompatible pointer type
    By bgalin in forum C Programming
    Replies: 2
    Last Post: 12-06-2009, 07:14 AM
  5. Replies: 3
    Last Post: 04-15-2008, 02:16 AM