Thread: warning: assignment from incompatible pointer type

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    warning: assignment from incompatible pointer type

    I want to print name and age of person in program

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
     
     typedef struct Person
       {
       char     Name[20];
       int      Age;
       int     *next;
       }List;
     
    int main()
       {
       List   *head = NULL;
    
    
       head = (List*)malloc(sizeof(List));
      
       head->Age = 20;
       strcpy(head->Name,"Abhi\n");
       head->next = NULL;
    
    
       while(!head)
          {
          printf(" %d ",head->Age);
          printf(" %s ",head->Name);
    
    
          head = head->next;
          };
    
    
       free(head);              
    
    
       return(0);
       }
    warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
    head = head->next;
    ^
    What's wrong in the code ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You declared next to be an int* instead of a struct Person*.

    Also, you should typedef the struct to either Person or Node, not List.
    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
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by laserlight View Post
    You declared next to be an int* instead of a struct Person*.

    Also, you should typedef the struct to either Person or Node, not List.
    Now code compile without warning but It doesn't print name and age

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
     
     typedef struct Person
       {
         char     Name[20];
         int      Age;
         struct  Person *next;
       }List;
       
    int main()
       {
       List   *head = NULL;
    
    
       head = (List*)malloc(sizeof(List));
      
       head->Age = 20;
       strcpy(head->Name,"Abhi\n");
       head->next = NULL;
    
    
       while(!head)
          {
          printf(" %d ",head->Age);
          printf(" %s ",head->Name);
    
    
          head = head->next;
          };
    
    
       free(head);              
    
    
       return(0);
       }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your while loop condition should be head, not !head. Actually, you shouldn't use head for this because you need it to keep track of the head of the linked list. Use another node pointer.
    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. Replies: 1
    Last Post: 04-09-2017, 02:11 PM
  2. warning: assignment from incompatible pointer type.
    By ingeniousreader in forum C Programming
    Replies: 4
    Last Post: 03-06-2012, 09:00 AM
  3. [Warning] Incompatible pointer type
    By dgs012 in forum C Programming
    Replies: 5
    Last Post: 02-20-2011, 11:27 AM
  4. Incompatible Pointer Type Warning
    By kwikness in forum C Programming
    Replies: 5
    Last Post: 10-30-2007, 06:14 PM
  5. warning: assignment from a incompatible pointer type
    By enderandrew in forum C Programming
    Replies: 8
    Last Post: 09-22-2007, 04:07 AM

Tags for this Thread