Search code examples
cparametersdeclarationidentifierfunction-declaration

How to define functions from declarations where the parameters do not have identifiers?


For an assignment, I was given a header file. The objective was to write the function definitions in a C file.

I am confused about how to write the definition for the functions when some of them do not have identifier names.

#include <stdint.h>

/** Course subjects. */
enum subject {

    SUBJ_ENGI,
    SUBJ_CIV,
    SUBJ_ECE,
    SUBJ_MECH,
    SUBJ_ONAE,
    SUBJ_PROC,
    SUBJ_CHEM,
    SUBJ_ENGL,
    SUBJ_MATH,
    SUBJ_PHYS,
};

struct course;

// Define the following functions:

struct course*  course_create(enum subject, uint16_t code);

enum subject    course_subject(const struct course*);

uint16_t    course_code(const struct course*);

void        course_hold(struct course*);

void        course_release(struct course*);

int     course_refcount(const struct course*);

I am not sure how I am supposed to define these functions when the prototypes do not have identifier names. For example, wouldn't it make more sense for the parameter to be const struct course* <Identifier> instead of just const struct course*?


Solution

  • Declarations are not definitions, the following code in the header file are declarations:

    struct course;
    enum subject    course_subject(const struct course*);
    

    You don't need to have parameter name in declarations, you need them in definitions. e.g. in implementation .c file, you could have:

    struct course {
        // add your fields
    };
    
    enum subject  course_subject(const struct course* c)
    {
        // access c's fields
    }
    

    in this case you will have to provide a name for the course parameter so that you can refer to it.