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*
?
CodePudding user response:
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.
CodePudding user response:
Parameter names are not needed in declarations because C matches arguments to parameters by position, not by name. Parameter names are needed inside a function definition so the body of the function has a way to refer to the function.
Define the functions using any parameter names you want. Simply repeat the declaration, insert whatever you want for the missing names, and replace the ;
that terminates the declaration by a {…}
compound statement that defines the function.