Caffeinated
Caffeinated

Reputation: 12484

In C, what is the minimum requirement to implement a header file?

I have a header file that starts like this:

typedef struct PriorityQueueStruct PriorityQueue;

/**
 * Type definition for an Element.
 */
typedef struct ElementStruct
{
    int element;
} Element;

/**
 * Type definition for a priority.
 */
typedef struct PriorityStruct
{
    int priority;
} Priority;

/**
 * Structure for containing the resultant values for a 
 *    call to 'createPriorityQueue'.
 *
 * 'token' is the identifier for the created data structure 
 *     and is to be used in any call to query or modify it.
 * 'resultCode' tells whether the function was successful or not in creating a 
 *     priority queue.  1 for success or an error code describing the failure.
 */
typedef struct InitializationResultStruct
{
        PriorityQueue* pq;
        int resultCode;
} InitializationResult;

/**
 * Structure for containing the resultant values for a 
 *    call to 'peek'.
 *
 * 'peekedValue' is the first-most value from the priority queue
 * 'resultCode' tells whether the function was successful or not in creating a 
 *     priority queue.  1 for success or an error code describing the failure.
 */
typedef struct PeekResultStruct
{
        Element element;
        Priority priority;
        int resultCode;
} PeekResult;

I am a bit lost on how to implement this header file in my simple class which is an application for a priority queue(called TriageQueue). Do I leave the structs in the header file alone? How do I go about implementing functions( thanks for tip! ) ? I appreciate any help!

Upvotes: 1

Views: 224

Answers (3)

Jason
Jason

Reputation: 32538

First, I suggest in your header file placing a "guard" that will prevent issues with compiler error messages based on duplicate definitions (if there are any in the header file). You can do this by placing the following C-preprocessor directives at the top of your header file:

#ifndef YOUR_HEADER_FILE_NAME_H
#define YOUR_HEADER_FILE_NAME_H

//...the contents of  your header file

#endif //<== Place this at the very end of the header.

Next, in your .c file, for simplicities sake, place the header file in the same directory as the .c file and then write the C-preprocessor directive #include "MyCodeFile.h" at the top of your .c file. You'll also need to include any other headers from the C standard library that you're using functions from such as printf, malloc, etc. Since these headers from the standard library will not be in the same directory as your .c file, you'll need to use angle-brackets with these headers, which instructs the compiler to look for the files among a set of search directories that have been defined in your development environment (most of this stuff should already be setup for your as they are in standard locations like /usr/include, etc.) For instance, #include <stdio.h> will include the I/O functionality from the C standard library.

Finally, in your .c file, once you've included the header file, simply create the definitions of your functions, etc. from the declarations you've created in the header file. For instance, if you have the following in your header file:

void function(int a, int b);

Then in your .c file, you would actually create the definition like:

void function(int a, int b) { ... }

In the case of structure-types like your current header includes, the "definition" would be an actual declaration of an instance of that structure type. This of course could take place inside a function, allocating the struct object on the stack, or it could be allocated statically as a global variable, or you could dynamically allocate memory for your struct object using malloc.

Upvotes: 1

m0skit0
m0skit0

Reputation: 25874

Implementation goes on .c, .cpp files.

About whether or not to include other definitions, it's up to you and your design. IMHO, if the structs have a relation with the class, I'll put them in the same header as the class.

Upvotes: 2

unwind
unwind

Reputation: 400109

C doesn't have classes or methods. It has functions.

And yes, assuming you must implement this particular interface, of course you need to leave the declarations alone.

It seems kind of broken though, the comments are referencing names that don't appear in the declarations (token and peekedValue).

Upvotes: 2

Related Questions