Thread: compilation error when attempting to set struct pointer

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

    compilation error when attempting to set struct pointer

    I've run into a bit of a snag with the following code that is perhaps a result of me not fully understanding how to deal with pointers in a struct.

    Code:
    typedef struct {
      int value;
      struct nodeT *next;
    } nodeT;
    
    int
    main(int argc, char* argv[]) {
    
      nodeT *node = malloc(sizeof(nodeT));
      nodeT *next = malloc(sizeof(nodeT));
    
      node->value = 5;
      node->next = next;
    
      return 0;
    
    }
    When I compile, I get the following error:

    program.c: In function 'main':
    program.c:17: warning: assignment from incompatible pointer type

    (this is the line highlighted and bolded in red above)

    What am I missing?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think the problem is that the struct is unnamed, so struct nodeT is a type unknown at the point where it is used to declare the next member pointer. You should be able to use nodeT as the name of the struct, and then keep the typedef to nodeT.

    By the way, you should #include <stdlib.h> for malloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    something like this:

    Code:
    typedef struct nodeT nodeT;
    
    struct nodeT {
      int value;
      nodeT *next;
    } ;
    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

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    OK. This is what I have (headers intentionally excluded). Same compile error.

    Code:
    typedef struct nodeT node;
    
    typedef struct {
      int value;
      node *next;
    } nodeT;
    
    int
    main(int argc, char* argv[]) {
    
      nodeT *node = malloc(sizeof(nodeT));
      nodeT *next = malloc(sizeof(nodeT));
    
      node->value = 5;
      node->next = next;
    
      return 0;
    
    }
    Maybe I'm not understanding this correctly. What's the difference between declaring a struct in the following manners:

    Code:
    typedef struct {
      ...
    } nodeT;
    Code:
    typedef struct Node {
      ...
    } nodeT;
    and

    Code:
    struct Node {
      ...
    }

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No, make an effort to name all your structs that are self-referring. struct { ... is anonymous, and can't have a pointer to itself as a data member.

    Like this:
    Code:
    struct node_tag {
       int value;
       struct node_tag * next;
    };
    
    typedef struct node_tag NodeT;
    I've typically been of the opinion that merging struct declaration and typedef's together are an obtuse exercise. It will work if you name it in the intermediary stage though, before it's named a new type.
    Last edited by whiteflags; 02-26-2008 at 01:58 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest using:
    Code:
    typedef struct nodeT {
      int value;
      struct nodeT *next;
    } nodeT;
    What's the difference between declaring a struct in the following manners:
    The first defines an unnamed struct. A typedef allows you to use nodeT as the name of the struct, but only after the struct's definition, not within it.

    The second defines a struct named Node, but you must use struct Node as the name of the struct. However, a typdef allows you to use nodeT as the name of the struct, instead of struct Node, but only after the struct's definition, not within it.

    The third defines a struct name Node, but you must use struct Node as its name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to struct
    By Paul Johnston in forum C Programming
    Replies: 4
    Last Post: 06-11-2009, 03:01 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM