C
Christopher Key
Hello,
I've a couple of situations where I'd like to have a function prototype
in a public API differing from the function definition.
Firstly, the API defines a callback function,
typedef void (*mycb)(int x);
Now, clients of this library might want to declare x as const in order
to add some compile time checking to their code. Clearly, their doing
this won't affect the library, nevertheless one compiler (although I'm
afraid I can't remember which it was) did issue a warning.
Secondly, the API defines a function,
void myfn(int *const x[]);
This is prototyped in this form, firstly because it best describes how x
is used, and secondly because it matches the actual form of the callback
mentioned above, which is closer to,
typedef void (*mycb)(int *const x[]);
Now, in order to add some compile time checking to my code, the function
is declared as,
void myfn(int *const *const x)
to stop me accidentally modifying x. Again, this change is entirely
irrelevant for the client, but I still get warnings about this.
I know that compilers are allowed to emit whatever diagnostics they
like, and the fact that this generates a warning is no indication of a
problem. Essentially what I'd like to know is whether doing this is:
1) Legal C and standard pratice
2) Legal C but bad practice
3) Illegal C
Regards,
Chris
I've a couple of situations where I'd like to have a function prototype
in a public API differing from the function definition.
Firstly, the API defines a callback function,
typedef void (*mycb)(int x);
Now, clients of this library might want to declare x as const in order
to add some compile time checking to their code. Clearly, their doing
this won't affect the library, nevertheless one compiler (although I'm
afraid I can't remember which it was) did issue a warning.
Secondly, the API defines a function,
void myfn(int *const x[]);
This is prototyped in this form, firstly because it best describes how x
is used, and secondly because it matches the actual form of the callback
mentioned above, which is closer to,
typedef void (*mycb)(int *const x[]);
Now, in order to add some compile time checking to my code, the function
is declared as,
void myfn(int *const *const x)
to stop me accidentally modifying x. Again, this change is entirely
irrelevant for the client, but I still get warnings about this.
I know that compilers are allowed to emit whatever diagnostics they
like, and the fact that this generates a warning is no indication of a
problem. Essentially what I'd like to know is whether doing this is:
1) Legal C and standard pratice
2) Legal C but bad practice
3) Illegal C
Regards,
Chris