compilation error when attempting to set struct pointer

This is a discussion on compilation error when attempting to set struct pointer within the C Programming forums, part of the General Programming Boards category; I've run into a bit of a snag with the following code that is perhaps a result of me not ...

  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
    21,593
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

    Code:
    typedef struct nodeT nodeT;
    
    struct nodeT {
      int value;
      nodeT *next;
    } ;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    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 12:58 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05: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

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