简体   繁体   中英

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?

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

struct Numbers;

int mult(struct Numbers *n);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM