Thread: Incompatible types in assignment

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    Incompatible types in assignment

    lists.c: In function `copyArrayToList':
    lists.c:53: error: incompatible types in assignment
    lists.c:61: error: incompatible types in assignment

    I'm getting this error when I compile but I'm not sure what I'm missing when I'm reading through it. Help would be great.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Small program to explore using singly linked list. */
    
    struct charRecord
    {
       char letter;
       struct charRecord *next;
    };
    
    typedef struct charRecord charNode;
    
    void copyArrayToList(charNode *start, char array[], int size);
    void printList(charNode *start);
    void freeList(charNode **start);
    charNode* newNode(char letter);
    void getString(char array[], int size);
    
    int main()
    {
       char array[10]; 
       charNode *start = NULL;
    
       getString(array, 10);
       copyArrayToList(&start, array, 10);
       printList(start);
       freeList(&start);
    
       return 0;
    }
    
    void getString(char array[], int size)
    {
       printf("Please enter %d characters\n", size-1);
       fgets(array, size, stdin); 
    }
    
    void copyArrayToList(charNode *start, char array[], int size)
    {
       /* Create a new linked list and copy the contents of 
          array into it. Make sure the list is in order
       */
    
       int i;
       charNode *temp, *prev, *loc;
    
       for (i=0; i<size; i++)
       {
          temp = newNode(array[i]);
          if (*start = NULL)
          {
             /* add 1st item to list */
             *start = *temp;
          }
          else
          {
             /* find correct location for item */
             loc = *start;
             prev = NULL;
             while (loc != NULL && loc->letter < array[i]) 
             {
                prev = loc;
                loc = loc->next;
             }
    
             /* insert item into list just before loc */
             temp->next = loc;
             if (prev == NULL)
             {     
                *start = *temp;
             }
             else
             {
                prev->next = temp;  
             }
          }
       }
    }
    
    void printList(charNode *start)
    {
       /* Print the contents of the list */
       
       while (start != NULL)
       {
          printf("%c", start->letter);
          start = start->next;
       }
       printf("\n");
    }
    
    void freeList(charNode **start)
    {
       /* Free up memory allocated to linked list */
    
       charNode *temp;
    
       while (*start != NULL)
       {
          temp = *start;
          *start = temp->next;
          free(temp);
       }
    }
    
    charNode* newNode(char letter)
    {
       /* dynamicaly allocate memory for an charNode
          make sure it is initialised correctly
       */
    
       charNode *temp;
    
       temp = (charNode *) malloc(sizeof(charNode));
       if (temp == NULL)
       {
          printf("WARNING - Memory allocation error\n");
          exit(EXIT_FAILURE);
       }
       temp->letter = letter;
       temp->next = NULL;
    
       return temp;
    }
    Last edited by Taka; 10-17-2006 at 05:14 AM.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Code:
     if ( *start = NULL )
    Should be

    Code:
    if ( *start == NULL )

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    EDIT: Sorry, ignore that its wrong I know! I didn't look at your code properly sorry lol

  4. #4
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Small program to explore using singly linked list. */
    
    struct charRecord
    {
       char letter;
       struct charRecord *next;
    };
    
    typedef struct charRecord charNode;
    
    void copyArrayToList(charNode *start, char array[], int size);
    void printList(charNode *start);
    void freeList(charNode **start);
    charNode* newNode(char letter);
    void getString(char array[], int size);
    
    int main()
    {
       char array[10]; 
       charNode *start = NULL;
    
       getString(array, 10);
       copyArrayToList(&start, array, 10); // I compiled with gcc, and this is wrong according to gcc, i reckon you don't need the & sign 
       printList(start);
       freeList(&start);
    
       return 0;
    }
    
    void getString(char array[], int size)
    {
       printf("Please enter %d characters\n", size-1);
       fgets(array, size, stdin); 
    }
    
    void copyArrayToList(charNode *start, char array[], int size)
    {
       /* Create a new linked list and copy the contents of 
          array into it. Make sure the list is in order
       */
    
       int i;
       charNode *temp, *prev, *loc;
    
       for (i=0; i<size; i++)
       {
          temp = newNode(array[i]);
          if (*start = NULL) //  maybe change it to start == NULL?
          {
             /* add 1st item to list */
             *start = *temp; //  here you say this
          }
          else
          {
             /* find correct location for item */
             loc = *start; 
             prev = NULL;
             while (loc != NULL && loc->letter < array[i]) 
             {
                prev = loc; //  different?
                loc = loc->next;
             }
    
             /* insert item into list just before loc */
             temp->next = loc;
             if (prev == NULL)
             {     
                *start = *temp;
             }
             else
             {
                prev->next = temp;  
             }
          }
       }
    }
    try correcting those
    Last edited by yxunomei; 10-17-2006 at 05:36 AM.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    I thought about changing it to == but wouldn't that cause a binary error?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incompatible types in assignment.
    By killpoppop in forum C Programming
    Replies: 36
    Last Post: 12-22-2008, 12:08 PM
  2. incompatible types in assignment
    By vapanchamukhi in forum C Programming
    Replies: 6
    Last Post: 09-19-2008, 07:45 AM
  3. incompatible types in assignment
    By vapanchamukhi in forum C Programming
    Replies: 1
    Last Post: 09-18-2008, 11:35 PM
  4. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  5. Incompatible types in assignment error
    By Zildjian in forum C Programming
    Replies: 12
    Last Post: 10-03-2003, 01:15 PM