Thread: Linked List compile error

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    29

    Linked List compile error

    I'm trying to write a basic little practice program to help me understand linked lists a little more (they give me trouble) and I'm trying to basically set it up to subtract every other node starting with the first, here is the code:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    struct ll 
    {
           int data;
           struct ll* next;
    }node;
    
    void print(struct ll *first);
    void subtractEveryOther(struct ll *first);
    
    int main () {
        
        struct ll* first;
        struct ll* second;
        struct ll* third;
        struct ll* fourth;
        struct ll* fifth;
        struct ll* six;
        
        struct ll* temp;
        
        first = (struct ll*)malloc(sizeof(struct ll*));
        second = (struct ll*)malloc(sizeof(struct ll*));
        third = (struct ll*)malloc(sizeof(struct ll*));
        fourth = (struct ll*)malloc(sizeof(struct ll*));
        fifth = (struct ll*)malloc(sizeof(struct ll*));
        six = (struct ll*)malloc(sizeof(struct ll*));
        temp = (struct ll*)malloc(sizeof(struct ll*));
        
        
        first->data = 5;
        
        first->next = second;
        second->data = 6;
        
        
        second->next = third;
        third->data = 7;
        
        third->next = fourth;
        fourth->data = 12;
        
        fourth->next = fifth;
        fifth->data = 23;
        
        fifth->next = six;
        six->data = 34;
        
        six->next = NULL;
        
        
        /////////////////////////    Heres the first compiling error, but if I comment out the 
       /////////////////////////     subtractEveryOther function, it compiles, but with the 
      /////////////////////////      error message shown. if subtractEveryOther is left alone
     /////////////////////////       it does not compile at all and shows an error message
    /////////////////////////        here as well as in the function which I'll highlight.
     
    
        temp = first->data;
        
        /////////////////////// The official error message is "assignment makes pointer 
        //////////////////////  from integer without cast".
    
    
    
        print(first);
        printf("temp = %d\n", temp);
        subtractEveryOther(first);
        
        
        
        
        system ("PAUSE");
        return 0;   
    }
    
    void print(struct ll *first) {
    
      while (first !=NULL) {
        printf("%d ", first->data);
        first = first -> next;
      }
      printf("\n");
    }
    void subtractEveryOther(struct ll *first) {
        
        int sum = 0;
        struct ll* temp;
        temp = (struct ll*)malloc(sizeof(struct ll*));
        
    //////////////////////////////////// This is the segment of code in the function which 
    //////////////////////////////////    gives the same error message as the one in main.
    
        temp = first->data;
    
    /////////////////////////// 
        
        while (temp != NULL) {
              sum-=temp;
              temp = first->next->next;
        }
         printf("sum = %d\n");
        
    }
    I don't understand why it compiles if the function is commented out yet does not when the function is left alone because both assignments of temp are the same. Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Here's the list of errors I get when I compile:
    Code:
    $ gcc -Wall list.c
    list.c: In function ‘main’:
    list.c:24: warning: implicit declaration of function ‘malloc’
    list.c:24: warning: incompatible implicit declaration of built-in function ‘malloc’
    list.c:61: warning: assignment makes pointer from integer without a cast
    list.c:69: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘struct ll *’
    list.c:75: warning: implicit declaration of function ‘system’
    list.c: In function ‘subtractEveryOther’:
    list.c:91: warning: incompatible implicit declaration of built-in function ‘malloc’
    list.c:96: warning: assignment makes pointer from integer without a cast
    list.c:101: error: invalid operands to binary - (have ‘int’ and ‘struct ll *’)
    list.c:104: warning: too few arguments for format
    You need to #include <stdlib.h> for malloc. You also need to free all the memory when you're done with it.

    The rest are related to the fact that you don't know whether temp is an int or a struct ll pointer:
    Code:
    temp = first->data
    temp is a pointer to a struct ll, first->data is an int. You can't just go assigning across types like that. What are you actually trying to do with that line?

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Hm I am using Dev C++ so maybe thats why you are getting additional errors, the only ones I get are related to
    Code:
     temp = first->data
    and I am basically trying to use temp as a way to travers the linked list without losing any of the other nodes.

    Code:
    temp = first->data;
    I thought that this line would start temp at the same place that first starts and from there I could just move and manipulate temp instead of the other nodes.
    Last edited by UCFuser; 04-26-2011 at 11:29 AM. Reason: bolded first to make it easier to understand

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by UCFuser View Post
    Hm I am using Dev C++ so maybe thats why you are getting additional errors
    No, it's more the fact that I put in that '-Wall' to enable all warnings. Depending on your compiler, the warning options are different, but it's always best to ensure your code compiles on the maximum setting with no warnings. You should also upgrade since Dev C++ is outdated and abandoned. Pelles C is a popular IDE and compiler. Some people use Code::Blocks as an IDE with MinGW as their compiler. You should Google those, see what suits you best and upgrade to something current.

    Code:
    temp = first->data;
    I thought that this line would start temp at the same place that first starts and from there I could just move and manipulate temp instead of the other nodes.
    Nope, that makes temp point to the address that is contained in first->data. But first->data is just an int that contains 5 in this case. 5 is generally not a good address for a pointer. You want it to point at first, not first->data:
    Code:
    temp = first;
    temp = temp->next;  // move to the next node in the list

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    I defenitly will upgrade compilers I've just been using the one recommended to me, but I've done some revising and now my program looks like this (i left out the initilization of the list but it is still at 5, 6, 7, ...
    Code:
     #include <stdio.h>
    
    
    struct ll 
    {
           int data;
           struct ll* next;
    }node;
    
    void print(struct ll *first);
    void subtractEveryOther(struct ll *first);
    
    int main () {
        subtractEveryOther(first);
        
        system ("PAUSE");
        return 0;   
    }
    void subtractEveryOther(struct ll *first) {
        
        int sum = 0;
        struct ll* temp = (struct ll*)malloc(sizeof(struct ll*));
       
        temp = first;
        
        while (temp != NULL) {
              
              temp->data = first->data;
              sum -= temp->data;
              /////////////////////////////////////////////////////
              temp->data = first->next;
             /////////////////////////////////////////////////////
        }
    printf("sum = %d\n", sum);
    }
    but it is not running the last print statement and I am now getting that "assignment makes pointer..." message where indicated in code, I think I may be more lost then when I started haha

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Gah!!!!!!!!! Pay attention to the type on both sides of the = sign. temp is a pointer to struct ll, so is first. But temp->data is an int, and first->next is a pointer to struct ll. They are not compatible, no matter how much you want them to be. I think you want something like:
    Code:
    sum = 0
    add = 1  // add the first element
    temp = first  // notice I dont malloc
    while temp != NULL
        if add
            sum += temp->data
        else
            sum -= temp->data
        add = !add  // toggle between add/subtract
        temp = temp->next
    print sum
    Don't malloc memory for temp. It's just pointing to things, you don't need to actually store stuff in it.

    After writing that explanation, it occurred to me that you might benefit from stopping working on this for a short while, until you better understand pointers and linked lists. We have some tutorials here and here, and Google has several more. Read your textbooks and class notes, and work all the examples until you have it down cold. Then come back to this.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    After drawing numerous pictures and looking over some of those explanations I think I understand much better now and the program works fine, heres my final function
    Code:
    int subtractEveryOther(struct ll *first) {
        
        int sum = 0;
        struct ll* temp;
       
        temp = first;
        
        while (temp != NULL) {
              
              sum -= temp->data;
              temp= temp->next->next;
        
        }
         return sum;
        
    }
    Not too much time to go over it though I take my final "Intro to C" exam in about 2 hours lol thanks for all the help though I appreciate it.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Glad you got it working and good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked list compile error
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2009, 12:43 AM
  2. linked list error....
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-09-2009, 08:56 PM
  3. Help with some error...(linked list)
    By google@ in forum C Programming
    Replies: 5
    Last Post: 11-08-2006, 08:29 AM
  4. Linked LIst error
    By Armatura in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2003, 11:40 PM
  5. linked list error
    By breanne in forum C++ Programming
    Replies: 2
    Last Post: 08-13-2002, 12:43 AM