Daft Question i think

This is a discussion on Daft Question i think within the C Programming forums, part of the General Programming Boards category; Hi There, Basically i have two .c files and a .h file One of the .c files contains a function: ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    61

    Daft Question i think

    Hi There,

    Basically i have two .c files and a .h file

    One of the .c files contains a function:
    Code:
    TransferMessage(int Port, struct Device *example)
    {
    example[Node].Address = ...
    blah
    blah
    blah
    }
    if the declaration for this function is at the top of the c file it is in it builds perfectly, however second i move it into the header file i get errors. The reason i need the declaration in the header file is because it is called from the other .c file.

    Thanks

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    >>> however second i move it into the header file i get errors
    What do you mean?

    Remember that it must be visible to parts of code that need this function.
    Also you said "error" , but i can not locate your description for it...

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    You may be mixing terms. Declaration is simply a description of the function, i.e. a prototype: the return type, name of the function, and all it's parameters. The definition includes the implementation or function body. More info here: What's the difference between declaring and defining in C and C++ - Cprogramming.com.

    If you're moving the definition into the header file that's wrong*. Keep variable and function definitions out of .h files. .h files just describe the name and type of a thing, they shouldn't reserve storage space for variables, or have actual code in them.

    All you need in the .h file is a function prototype, or declaration. Something like this:
    Code:
    extern TransferMessage(int Port, struct Device *example);
    That extern keyword is to notify the linker** that the definition of the function will be in some other .c file. Thus, if the definition is in foo.c, and you #include this header in bar.c, the linker will see this extern function called TransferMessage. It wont worry about not being able to find it right away, in the same file. It will know that function exists externally to bar.c, in some other file.
    That semicolon is there just to tell the compiler there is no function body (definition) to follow, just a declaration.

    One more note, all your header files should have include guards (link). They keep the compiler from complaining about multiple declarations, by preventing the same .h file from being included multiple times in the same translation unit. A translation unit is a technical term that is basically a .c file.

    EDIT:
    * It is okay to have the definition in a header file in some rare circumstances, most notably if you "inline" the function. That is a trick to help make your code faster, but it is something you should not bother with 99.9% (or more) of the time.

    ** The linker was traditionally a separate component from the compiler proper. It is still a separate entity in many implementations, but now the compiler usually handles both stages seamlessly. The compiler proper would turn a .c file into an object file (often suffixed .o), which contained (roughly) the machine code for the .c file. The linker would combine that object file with any other object files you specify, as well as libraries and "startup" and "cleanup" code. There is often code that runs before and after your main function, that sets up and cleans up behind the scenes stuff that you usually don't need to worry about.
    Last edited by anduril462; 11-09-2012 at 08:53 AM. Reason: added info about inlining functions

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Maybe it is because of struct Device? Also you need a return type to avoid "implicit declaration" in C90 mode and errors in C99 mode.

    Here is a "cookbook" approach:

    Header file transfermessage.h:
    Code:
    #ifndef TRANSFERMESSAGE_H
    #define TRANSFERMESSAGE_H
    #include "struct_device.h"
    
    int TransferMessage(int Port, struct Device *example);
    
    #endif
    Implementation file transfermessage.c:
    Code:
    #include "transfermessage.h"
    #include "struct_device.h"
    
    int TransferMessage(int Port, struct Device *example)
    {
        // TODO
        return 0;  
    }
    Main program main.c:
    Code:
    #include <stdio.h>
    #include "transfermessage.h"
    #include "struct_device.h"
    
    int main()
    {
        // struct_device.h should provide some function which gives
        // back a valid initial struct Device object
        struct Device d = device_init(); 
    
        int retval = TransferMessage(1234, &d);
        printf("TransferMessage returned code %d\n", retval);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Daft Question
    By r_james14 in forum C Programming
    Replies: 3
    Last Post: 03-23-2012, 05:37 AM
  2. Daft Question Time
    By r_james14 in forum C Programming
    Replies: 6
    Last Post: 11-18-2011, 09:02 AM
  3. Everyone who reads this will laugh, such a daft question.
    By r_james14 in forum C Programming
    Replies: 8
    Last Post: 11-07-2011, 06:22 AM
  4. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  5. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM

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