Thread: why i get this error..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    why i get this error..

    in the end i want elt to point on some address
    why i get
    c(28) : error C2223: left of '->next' must point to struct/union
    but when i change it to
    Code:
    (*p)->next->next;
    it works fine,why i need these cols
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct node {
        int value;
        struct node *next;
    }Node;
    
    void what1(Node ** p,int num);
    int main()
    {
       Node *p;
       p=(Node *)malloc(sizeof(Node));
       p->value=1;
       p->next=(Node *)malloc(sizeof(Node));
       p->next->value=2;
       what1(&p,0);
       free(p);
        return 0;
    }
    
    void what1(Node ** p,int num)
    {
       Node *elt;
       elt=(Node*)malloc(sizeof(Node));
       elt->next=*p;
       elt->value=num;
        *p=elt;
    	elt=*p->next->next;
    }
    Last edited by transgalactic2; 03-21-2009 at 02:54 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because, as the error message pointed out, p does not point to a struct or a union. It points to a pointer.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Why do you think you need the dereferencing operator "*" in the first place?

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so why the cols makes it wright?
    works:
    elt=(*p)->next->next;


    doesnt work

    elt=*p->next->next;

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    so why the cols makes it wright?
    works:
    elt=(*p)->next->next;


    doesnt work

    elt=*p->next->next;
    read about operator precedence and binding first!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM