trouble with structs..

This is a discussion on trouble with structs.. within the C Programming forums, part of the General Programming Boards category; Given the code: Code: typedef struct { int item; struct node *next; } node; int main(int argc, char *argv[]) { ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    69

    trouble with structs..

    Given the code:

    Code:
    typedef struct {
      int item;
      struct node *next;
    } node;
    
    int
    main(int argc, char *argv[]) {
    
      node *n = malloc(sizeof(node));
    
      n->item = 5;
      n->next = n;
    
      return 0;
    
    }
    (yes, I'm aware that in this configuration, the node will be pointing to itself).

    Why does gcc (OSX 10.5) report the following error:
    josephus.c: In function 'main':
    josephus.c:15: warning: assignment from incompatible pointer type

    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Did you ever define a struct node? You have an anonymous struct that you have typedef'ed as a node. Perhaps try
    Code:
    typedef struct Node {
        int item;
        struct Node *next;
    } node;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Replies: 7
    Last Post: 06-04-2008, 10:39 PM
  3. Replies: 41
    Last Post: 07-04-2004, 03:23 PM
  4. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21