compilation error -

This is a discussion on compilation error - within the C Programming forums, part of the General Programming Boards category; hi guys... i am having some trouble implementing an insert function for a linked list... this is my code so ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    21

    compilation error -

    hi guys... i am having some trouble implementing an insert function for a linked list...

    this is my code so far..

    Code:
    //header file
    typedef struct{
        char *comName;
        int comValue;
    }fcommand;
    
    
    typedef struct{
        fcommand* command;
        struct node *next;
    }node;
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #include "linkedlist.h"
    
    //insertNode Function 
    void insertNode(void){
        node *newNode = (node*)malloc(sizeof(node));
        char* string = "Hello";
        //char* name;
        //initialize node
        newNode->next = NULL;
        
        //instantiate command
        newNode->command = (fcommand*)malloc(sizeof(fcommand));
        
        if (newNode->command == NULL){
            free(newNode);
            return;
        }
        
        //initialize command (TESTING)
        newNode->command->comName = string;
        
        //check for empty list
        if (head == NULL){
            head = newNode;
        }
        //check if the command belongs at the lists head.
        else if (head->next == NULL){
            newNode->next = head->next;
            head->next = newNode; //assignment from incompatible pointer type
            printf("added at the beginning.. \n");
        }
        //loop through the list and add the command at the end of the list
        else{
            node *current  =head;
            while(current->next != NULL){
                current = current->next; //assignment from incompatible pointer type
            }
            current->next = newNode; //assignment from incompatible pointer type
            printf("Added at the end.!\n"); 
        }
    }
    i know it is not the best code.. butt your help will be appreciated.. !

    thanks guys.
    Last edited by m_programmer; 09-15-2012 at 01:38 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Have you actually tried to READ the error messages? The text of error messages usually has some relationship to the problem. That is why compilers output error and warning messages, rather than just printing an uninformative "Error 42 - Your program universe has no meaning"

    Visually (I haven't bothered to feed the code to a compiler) I can see your insertNode() function makes use of a variable called head. That variable is not declared anywhere. C compilers complain bitterly about code that uses variables that have not been declared.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    yeah sorry i forgot to include it here...

    node *head = NULL; //declare head

    yes, i have read the error messages,, but i cant seem to figure why am i getting those errors.. (

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    FIXEDDDD.............!

    thanks guys
    Last edited by m_programmer; 09-15-2012 at 01:54 AM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Quote Originally Posted by m_programmer View Post
    yeah sorry i forgot to include it here...

    node *head = NULL; //declare head

    yes, i have read the error messages,, but i cant seem to figure why am i getting those errors.. (
    Let me see if I understand your approach in this thread.

    You post code that is different from what exhibits your problem. You don't understand the error messages, but don't bother to post any relevant information about those error messages.

    I suggest you read this link before posting again, and learn how to ask questions in a manner that increases your chances of getting a useful response.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    Quote Originally Posted by grumpy View Post
    Let me see if I understand your approach in this thread.

    You post code that is different from what exhibits your problem. You don't understand the error messages, but don't bother to post any relevant information about those error messages.

    I suggest you read this link before posting again, and learn how to ask questions in a manner that increases your chances of getting a useful response.
    thanks for the response grumpy..
    well i just forgot to include one declaration .

    and i did put information about the error messages .. if you look at my code ,, it is commented next to the line that is giving me the errors.. ! for example:
    current = current->next; //assignment from incompatible pointer type
    the only reason i posted this thread was that i didnt understand how to solve those errors and was hoping someone would help me out..! but anyhow i fixed the errors now.. ! D

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Quote Originally Posted by m_programmer View Post
    well i just forgot to include one declaration .
    You believe it is appropriate to leave relevant information out when asking for help?

    Quote Originally Posted by m_programmer View Post
    and i did put information about the error messages .. if you look at my code ,, it is commented next to the line that is giving me the errors.. ! for example:
    current = current->next; //assignment from incompatible pointer type
    The version of the post I responded to did not include those comments. I assume you edited them in later.

    Even if you had included the comments up front, comments in source code are not an ideal way to highlight errors, because people who are looking for errors in code will often naturally ignore comments. If you are going to do things that way, explicitly tell people that - outside the code.
    Last edited by grumpy; 09-15-2012 at 06:40 AM.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: was not declared in this scope compilation error
    By i_r_tomash in forum C Programming
    Replies: 2
    Last Post: 05-27-2010, 07:44 AM
  2. compilation error
    By blue_gene in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2004, 07:26 AM
  3. compilation error
    By blue_gene in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2004, 09:48 AM
  4. help about compilation error
    By netwizio in forum Linux Programming
    Replies: 2
    Last Post: 01-16-2004, 05:22 PM
  5. Error with compilation
    By mattbrrtt in forum C Programming
    Replies: 0
    Last Post: 01-08-2002, 03:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21