Thread: General function prototype and header files question

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    84

    General function prototype and header files question

    Say you declare in your header a function prototype and an argument of it is a structure that is in another header file. Do you have to have that type definition of that structure in this header file where the function prototype is located or does it not matter?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by towed View Post
    Say you declare in your header a function prototype and an argument of it is a structure that is in another header file. Do you have to have that type definition of that structure in this header file where the function prototype is located or does it not matter?
    Yep! you need the header file that typedefs the struct otherwise the compiler will barf.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by towed
    Say you declare in your header a function prototype and an argument of it is a structure that is in another header file. Do you have to have that type definition of that structure in this header file where the function prototype is located or does it not matter?
    No, you do not need the definition of that structure in that header file. However, you do need a declaration of that structure in that header file, before the declaration of that function, and then you need the definition of that structure in the source file that defines that function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    No, you do not need the definition of that structure in that header file. However, you do need a declaration of that structure in that header file, before the declaration of that function, and then you need the definition of that structure in the source file that defines that function.
    ..... if that function accesses any member of any instance of that structure.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Do you have to have that type definition of that structure in this header file where the function prototype is located or does it not matter?
    You don't need to have. Here is an example.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    I don't know. I had a problem with my code compiling and after switching the function prototype to the other header file (where the type definition is), it finally compiles. Which would suggest it has to...?

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by towed View Post
    I don't know. I had a problem with my code compiling and after switching the function prototype to the other header file (where the type definition is), it finally compiles. Which would suggest it has to...?
    That is the easiest and most obvious way. If you have a header file that you include with function prototypes that contain references to a so far undefined datatype, of course there is an issue. If the header file with prototypes is read in before the actual structure is defined, you can include a structure "prototype" (ie, declaration) with the function prototypes. Eg:
    Code:
    struct something {
      [blah blah]
    };
    is the definition.
    Code:
    struct something;
    is a declaration with no definition. Since function prototypes do not contain references to struct members, etc, the struct does not have to be defined, it only has to be declared for them to pass the compiler.

    Obviously, the struct does have to be fully defined before the functions are, but that could be in a subsequently included file or in a pre-compiled linked object.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C interview questions
    By natrajdreams in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 12:40 PM
  2. Replies: 2
    Last Post: 10-23-2007, 11:10 AM
  3. Function Prototype & header file
    By sarwansan in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 03:54 PM
  4. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM
  5. Header Files
    By CSK in forum C Programming
    Replies: 8
    Last Post: 10-25-2001, 01:18 AM