Thread: Passing functions between files

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    23

    Passing functions between files

    Hi, I am kind of new to C programming and I got two different files. One is my main .c file which I am using to make my main program and I got another file with an extension of .h. The .h file has some some functions built in as well as declared variables. I am trying to make use of these functions in my main .c file.

    I included this line on top of my main .c file

    #include "file.h"

    This line should include the .h file on my main program and also give it all the functionality and variables, but I am still unclear on how to make use of them. Can anybody point me in the right direction?

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't put functions in header files. You put function prototypes in header files. You put functiosn in .c files. You #include the header which references those functions wherever you want to use them. Just like when you use printf you include the correct header for it wherever you want it used.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    OK, perhaps I should make myself clearer.

    My header file looks like the following:

    Code:
    #ifdef _PUZZLE_H
    #define _PUZZLE_H
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define COLS 3
    #define ROWS 2
    
    void movePuzzle(int puzzle[][COLS], char dir, int rowcol);
    void initPuzzle(int puzzle[][COLS]);
    
    void initPuzzle(int puzzle[][COLS])
    {
       /* Initialise the puzzle and shuffle it around */
    
       int x, y;
       unsigned int seed;
       char temp;
    
       /* set up random number generator */
       printf("Please enter puzzle seed number : ");
       x = scanf("%u", &seed);
       if (x != 1)
       {
          printf("invalid input for seed\n");
          exit(0);
       }
       srand(seed);
    
       /* clear keyboard of newline */
       do (scanf("%c", &temp)); while (temp != '\n');
    
       /* fill puzzle */
       for (y=0; y<ROWS; y++)
       {
          for (x=0; x<COLS; x++)
          {
             puzzle[y][x] = 1 + y * COLS + x;
          }
       }
    
       /* shuffle puzzle */
       for (y=0; y<=ROWS*COLS; y++)
       {
          if ((rand() % 2) == 0)
          {
             movePuzzle(puzzle, 'v', rand() % COLS);
          }
          else
          {
             movePuzzle(puzzle, 'h', rand() % ROWS);
          }
        }
    }
    
    #endif
    No I am trying to call the initPuzzle function from this header file to my source file by doing the following on my .c file:

    Code:
    #include "puzzle.h"
    
    int main()
    {
            initPuzzle(int puzzle[][COLS]);
    
    	return 0;
    }
    It gives me a list of several errors, however. Anyone know what I am doing wrong and how I can make these functions work?

    Thanks

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't do it that way.
    Re-read quzah's post.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Sorry, I am just not sure of which way I should do it. Problem is I cannot modify the contents of the header file at all and I need to call the functions to the main c file.

  6. #6
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    If you want them included, why not just make a separate .c file?

    Code:
    // common includes like stdio.h or time.h
    #include "header.h" // your headers - defines, includes, and that sort of stuff
    #include "commons.c" // your commonly used functions
    
    // code for your main .c file

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Review:

    Code:
    #include "puzzle.h"
    
    int main()
    {
            initPuzzle(int puzzle[][COLS]);
    
    	return 0;
    }
    Thats not how you pass args in C, nor how you declare variables.

    perhaps declare puzzle before trying to use it as an arg?

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Quote Originally Posted by ulillillia View Post
    If you want them included, why not just make a separate .c file?

    Code:
    // common includes like stdio.h or time.h
    #include "header.h" // your headers - defines, includes, and that sort of stuff
    #include "commons.c" // your commonly used functions
    
    // code for your main .c file
    I do not want to add any more files. I just need a single .c file that gets the functions from the header file.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The generally accepted way to do it is to have a header file that contains prototypes, and a matching .c file with all of the implementations of the functions. You could run into other problems otherwise.

  10. #10
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by Duskan View Post
    I do not want to add any more files. I just need a single .c file that gets the functions from the header file.
    I don't think C cares what you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  2. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  3. passing structs to functions?
    By Neildadon in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2002, 04:31 PM
  4. Passing structures between functions
    By TankCDR in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2002, 10:54 AM
  5. Passing data/pointers between functions
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 12:59 AM