Thread: struct node warning

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    24

    struct node warning

    Code:
    #include <stdio.h>
    
    typedef struct{
    	struct name *studentName;
    }student;
    
    
    typedef struct{
    	char *firstName;
    	char *secondName;
    }name;
    
    
    int main(void){
    	student *a;
    	name *b;
    	
    	b=(name*)malloc(sizeof(name));
    	a=(student*)malloc(sizeof(student));
    	
    	b->firstName="david";
    	b->secondName="becks";
    
    	a->studentName=b;  //warning line
    }
    Hi,i wrote above a simple struct . the student struct has a link to another name struct to store the 1st and 2nd name.

    The code works but i get a warning at the above stated line.
    warning: incompatible types -from 'name*' to 'name *'

    I hope someone can correct my error.

    much apreciated.
    Thanks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    OK, here's your main problem:

    Code:
    struct name *studentName;
    Where is "struct name" defined or declared? The answer might surprise you: It hasn't.

    Later on you typedef an anonymous struct to be known as name.

    Solution? Re-arrange your structs:

    Code:
    typedef struct{
    	char *firstName;
    	char *secondName;
    }name;
    
    typedef struct{
    	name *studentName;
    }student;
    Otherwise:

    • Actually return a value from main(). According to popular convention, 0 is used to signify success, while non-zero means failure of some sort.
    • Check if malloc() fails before you use dynamically allocated memory.
    • Include stdlib.h if you use malloc(). If you had warnings turned up, you probably would have been alerted that you missed it:

      Code:
      : warning: implicit declaration of function `malloc'

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    struct name *studentName is not the same thing as name *studentName. I don't know why it didn't emit an error for an undeclared struct though.
    Code:
    #include <stdio.h>
    #include <stdlib.h> /* for malloc */
    
    typedef struct{
    	char *firstName;
    	char *secondName;
    }name;
    
    
    typedef struct{
    	name *studentName;
    }student;
    
    
    
    int main(void){
    	student *a;
    	name *b;
    	
    	b=(name*)malloc(sizeof(name));
    	a=(student*)malloc(sizeof(student));
    	
    	b->firstName="david";
    	b->secondName="becks";
    
    	a->studentName=b;
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    Angry trying to make into list of students

    thank you for your help.I have then proceeded to make it into a list of students.I get an error again.I dont know what wrongs

    Code:
    #include <stdio.h>
    #include <stdlib.h> /* for malloc */
    
    typedef struct student* studentPtr;
    
    typedef struct{
    	char *firstName;
    	char *secondName;
    }name;
    
    
    typedef struct{
    	name *studentName;
    	studentPtr nextStudent;
    }student;
    
    
    
    int main(void){
    	name *a,*b;
    	student *c,*d;
    	
    	a=(name*)malloc(sizeof(name));
    	b=(name*)malloc(sizeof(name));
    	c=(student*)malloc(sizeof(student));
    	d=(student*)malloc(sizeof(student));
    	
    	a->firstName="david";	a->secondName="becks";
    	b->firstName="Eric";	b->secondName="Olmado";
    
    	c->studentName=a;
    	d->studentName=b;
    
    	c->nextStudent=d;   /*warning line*/
    	return 0;
    }
    i get the warning of incompatible type.
    tried hard to figure it out though.maybe my basic is wrong

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're doing the same wrong thing.

    studentPtr is struct student *.
    d is of type student *.
    struct student * is not not not the same thing as student *.

    Until you get this through your head, you're going to continue making the same mistakes. Instead of just copying the code I provided in my first post, go through my explanation and try to understand it. That should make it more clearer as to why your above code is wrong.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you did this:
    Code:
    typedef struct student* studentPtr;
    before this:
    Code:
    typedef struct{
    	name *studentName;
    	studentPtr nextStudent;
    }student;
    the typedef doesn't know what struct student is, so it has problems.
    you have to define the types that you use before you use them. this includes other syntaxes like when you use typedef.


    normally, in C, you have to prepend a 'struct' keyword in front of struct things because that's the syntax. You can get around that with using the typedef, but there are some gotchas to that.

    Code:
    struct s
    {
        int a;
    };
    
    struct s my_s; /*legal*/
    
    struct b{char my_char;};
    
    b my_b;/*not legal*/
    
    typedef struct b b;
    
    b my_b;/*now legal*/
    
    typedef struct {char my_char;}c;
    
    struct c my_c;/*not legal*/
    c my_c;/*legal*/
    
    typedef struct d{char my_char;}d;
    
    struct d my_d;/*legal*/
    d my_d;/*legal*/
    and just to throw you off:
    Code:
    struct e {
        int my_int;
    } my_instance;
    
    my_instance i; /*not legal*/
    
    int main()
    {
        struct e i; /*legal*/
    
        i.my_int = 234; /*legal */
        my_instance.my_int = 3254;/*legal*/
    
        return 0;
    }
    maybe just put
    Code:
    typedef student* studentPtr;
    after
    Code:
    typedef struct{
    	name *studentName;
    	studentPtr nextStudent;
    }student;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  5. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM