sggsayan93
sggsayan93

Reputation: 53

C Function Prototype With Struct Argument

I want to write a function prototype for a function, whose argument is a pointer to a struct.

int mult(struct Numbers *n)

However, the struct Numbers, which is defined as

struct Numbers {
    int a;
    int b;
    int c;
};

is not defined yet. How should I write a suitable prototype for mult?

Upvotes: 5

Views: 12601

Answers (2)

Jack
Jack

Reputation: 133557

You must forward the declaration of the structure to tell the compiler that a struct with that name will be defined:

struct Numbers;

int mult(struct Numbers *n) {

}

struct Numbers {
    int a;
    int b;
    int c;
};

Mind that the compiler is not able to determine the size in memory of the structure so you can't pass it by value.

Upvotes: 4

ouah
ouah

Reputation: 145829

Just declare struct Numbers as an incomplete type before your function declaration:

struct Numbers;

int mult(struct Numbers *n);

Upvotes: 5

Related Questions