Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

c - Two supposedly compatible function pointer types is being warned as incompatible. What rule am I breaking?

I have a set of functions for performing unsigned modular multiplication over large integers. For some reason, when I use my generic remainder function in the multiplication, the compiler (I'm using Clang with -Wall -Wextra warning flags) gives incompatible pointer warning.

Header:

typedef struct {
    uint32_t c;
    uint32_t v[];
} vlong_t; // generic incomplete type.

vlong_t *vlong_remv_inplace(vlong_t *rem, const vlong_t *b);

typedef void *(*vlong_modfunc_t)(
    vlong_t *restrict v,
    void *restrict ctx);

vlong_t *vlong_mulv(
    vlong_t *restrict out,
    const vlong_t *a,
    const vlong_t *b,
    vlong_modfunc_t modfunc,
    void *restrict mod_ctx);

Test code section that's giving the warning:

vlong_mulv(x, a, b, vlong_remv_inplace, b);

I've simplified the code a bit to create a minimal working example, my coding style have different variable naming conventions among others.

The warning is as follow:

vlong-test.c:85:20: warning: incompatible pointer types passing 'vlong_t *(vlong_t *, const vlong_t *)' to parameter of type 'vlong_modfunc_t'
      (aka 'void *(*)(vlong_t *restrict, void *restrict)') [-Wincompatible-pointer-types]

vlong_mulv(x, a, b, vlong_remv_inplace, b);
                    ^~~~~~~~~~~~~~~~~~

What rule am I breaking here?

question from:https://stackoverflow.com/questions/65652186/two-supposedly-compatible-function-pointer-types-is-being-warned-as-incompatible

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The function vlong_remv_inplace must have the same signature as the type vlong_modfunc_t so in vlong_remv_inplace you need to replace the type of the second parameter and the result with a void pointer:

void *vlong_remv_inplace(vlong_t *rem, void *b);

The alternative is to change the signature of the type vlong_modfunc_t to match the signature of the function vlong_remv_inplace.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...