#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Unhappy Dereferencing pointer to inclomplete type HELP


    #include<stdio.h>
    #include<stdlib.h>
    typedef struct {
    int data;
    struct node * link}node ;
    int main()
    {
    node *first,*second;
    first=(node *)malloc(sizeof(node));
    second=(node *)malloc(sizeof(node));
    first->link=second;
    first->link->data=5;
    }
    Gives error in the last line i dont know why
    works fine with turboc compiler though
    gcc throws an error saying dereferencing pointer to incomplete type
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    You need to tag your struct. Note the "node" after "typedef struct"
    There also should be a semicolon after link

    Code:
    typedef struct node {
        int data;
        struct node * link;
    } node ;
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    It should be like this
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    typedef struct node {
      int data;
      struct node *link;
    } node;
    
    int main()
    {
      node *first, *second;
      first = malloc(sizeof(node));
      second = malloc(sizeof(node));
      first->link = second;
      first->link->data = 5;
      return 0;
    }
    Some things to note:
    1. The struct node on line 3 declares the name of the struct that can be used on line 5.
    2. The cast on the return result of malloc is removed. It does nothing useful in valid C programs compiled with valid C compilers.

    You should also get rid of that fossil TurboC, and grab one (or more) of these modern compilers.
    http://www.microsoft.com/express/Downloads/
    http://www.smorgasbordet.com/pellesc/
    http://www.codeblocks.org/
    http://sourceforge.net/projects/orwelldevcpp/

    > gcc throws an error saying dereferencing pointer to incomplete type
    A much better idea - stick to using gcc from now on.
    I would recommend starting with
    gcc -Wall -Wextra -Werror prog.c
    when compiling code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    You're missing a semi-colon after link .
    main should choose the value it returns to the operating system.
    Did malloc succeed?
    And I think you don't know how to use typedef with structs. Neither do I. Having learned to use the preprocessor instead of typedef I've changed your program as follows, which still doesn't check for malloc success.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define NODE struct node
    NODE {
      int data;
      NODE*link;
    };
    
    int main() {
      NODE*first,*second;
      first=malloc(sizeof(NODE));
      second=malloc(sizeof(NODE));
      first->link=second;
      first->link->data=5;
      return 0;
    }

    Comments on this post

    • salem disagrees : Simply awful use of #define :(
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo