DeiDei DeiDei - 1 year ago 77
C Question

Specifying pointers as restrict only in declaration?

So my question is this. If in a header file I have a function declaration:

extern void func(void* restrict, void* restrict);

and then in a source file, I define it as such:

void func(void*, void*) {}

is this a problem? Are the
qualifiers lost?

P.S. This is so I can compile the source file in C89 mode and change the prototype in the header to the respective C89/C99 version with a conditional macro.

Answer Source

"This is so I can compile the source file in C89 mode" - Simple reason: restrict is not a reserved keyword until C99 see forword of C11 (C99 is the 2nd edition), so it will just be used as a name, which is ignored in the prototype.

But both function declarators (prototype and definition) have to specify the same type, i.e. restrict is required in both.

You have to compile the header and the implemenation with the correct C version. For restrict the definition is typically more relevant than the prototype, but the compiler might be able to detect violations in the caller. Always assume relying on such hacks breaks your code.

After a the comments, trying a bit of clairvoyancy:

If you want to make the code compile with ancient C90, yet take advantage of newer features where useful, you can use a macro:

#if this_is_c99_or_c11
#define RESTRICT retrict
#define RESTRICT

void f(int * RESTRICT p);


void f(int * RESTRICT p)

Still remember there can be problems cross-version compiling caller and callee. Check your target's ABI.