Thread: Why are my functions not working?

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    117

    Why are my functions not working?

    Ok my assignment is to teach us how to put bits of programs together.

    One program is supposed to have just the main.
    Another program has all of the functions that main calls.
    Then there is a header that holds the function headers.

    So far my main is just this...

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include "bin-library.c"
    #include "bin-library.h"
    
    
    int main(void)
    {
        struct node *root = NULL;
        int *array_of_numbers;
        int Max_lines = 0;
        int i;
        
        array_of_numbers = numbers(&Max_lines);
        array_of_numbers = sort_array(array_of_numbers, Max_lines);
        
    getchar();
    return 0;
    }
    My functions are in this program....(I'm going to edit out the functions that are working and just show you the one I'm having problems with to save space.


    Code:
    #include "bin-library.h"
    
    struct node *addNode( int x ){
        struct node *new_node;
        new_node = malloc( sizeof(struct node) );
        
        new_node->value = x;
        new_node->left = NULL;
        new_node->right = NULL;
    
    
        return new_node;
    }
    And the header is here (again editing out the functions that work)

    Code:
    #include "bin-library.c"
    
    struct node {
        int value;
        struct node *left;
        struct node *right;
    };
    
    
    struct node *addNode( int x );

    So I think I have the #include the stuff set up right. ( I don't even think I need the last #include "bin-library.c"

    But when I run my code (notice I'm not calling the function yet, it's just declared...

    My function is giving me several warnings.

    1.) invalid application of 'sizeof' to incomplete type 'node'
    2.) dereferencing pointer to incomplete type ( this one is given three times, for the new_node->value, new_node->left, and new_node->right.

    It's like it's not seeing the header. Any ideas what I'm doing wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You should never have #include "some_file.c" in your files.

    Source files should be listed in your makefile / project.

    The .h file should be included wherever you need it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Ok so how will my bin-library.c be connected to the main program?
    The header consists of the declaration of the functions yes?
    Then part of the homework says...
    "You will create your own library of binary tree functions. That is, create functions for addingnodes, balancing the tree, traversing the tree in preorder, etc. These functions should be in
    their own file with the .c extension."

    So that would be the bin-library.c right?
    Store my functions there.
    It says include the header file in both of the .c which I have.

    So I don't need to include anything at the top of the header file other than the file declarations right?
    Confused on how they are all going to connect then.

    Edit.

    I just reread what you said.
    So I'm not running this on gcc yet. Just on Dev-C. Do I need to run in on gcc with my Makefile and it should work?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    In an IDE, you need to go into something like project->settings then find somewhere where there is a list of source files.

    There might even be a "project" pane on the left/right side.

    You'll find at least ONE source file listed there, from when the project was created.

    Add your other source files to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with functions
    By themsaman in forum C Programming
    Replies: 16
    Last Post: 03-18-2010, 12:25 PM
  2. Functions Not Working...
    By dac in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2006, 01:41 PM
  3. working with functions
    By robasc in forum C++ Programming
    Replies: 13
    Last Post: 12-27-2005, 01:38 PM
  4. Functions are not working
    By founder247184 in forum C++ Programming
    Replies: 0
    Last Post: 11-29-2002, 04:00 PM