How to fix "incompatible pointer type" error in C?

I have homework about function pointers and implement inheritance in C. I got "incompatible pointer type" error about Aralik, VerilenlerArasindan and Harfler. What's problem?

RastgeleKarakter.h

    struct RASTGELEKARAKTER{
        Random ran;
        char (*Harf)(struct RASTGELEKARAKTER*);
        char* (*Harfler)(struct RASTGELEKARAKTER*);
        char* (*Aralik)(struct RASTGELEKARAKTER*);
        char* (*VerilenlerArasindan)(struct RASTGELEKARAKTER*);
        char* (*Cumle)(struct RASTGELEKARAKTER*);
        void (*YokEt)(struct RASTGELEKARAKTER*);
    };

    typedef struct RASTGELEKARAKTER* RastgeleKarakter;

    RastgeleKarakter RastegeleKarakterOlustur();
    char RandomHarf(RastgeleKarakter);
    char* RandomHarfler(RastgeleKarakter,unsigned);
    char* RandomAralik(RastgeleKarakter,unsigned,char,char);
    char* RandomVerilenlerArasindan(RastgeleKarakter,unsigned,char*);
    char* RandomCumle(RastgeleKarakter);
    void RastgeleKarakterYokEt(RastgeleKarakter);

RastgeleKarakter.c

RastgeleKarakter RastegeleKarakterOlustur(){

    RastgeleKarakter karakter;
    karakter = (RastgeleKarakter)malloc(sizeof(struct RASTGELEKARAKTER));
    karakter->ran = RandomOlustur();
    karakter->Harf = &RandomHarf;
    karakter->Harfler = &RandomHarfler;
    karakter->Aralik = &RandomAralik;
    karakter->VerilenlerArasindan = &RandomVerilenlerArasindan;
    karakter->Cumle = &RandomCumle;
    karakter->YokEt = &RastgeleKarakterYokEt;
    return karakter;
}

2 answers

  • answered 2019-04-21 10:21 bruno

    warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] karakter->Harfler = &RandomHarfler;

    because

    char* (Harfler)(struct RASTGELEKARAKTER);

    but

    char* RandomHarfler(RastgeleKarakter,unsigned);

    RandomHarfler gets two argument but Harfler must receive a pointer to a function getting only a struct RASTGELEKARAKTER*


    warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] karakter->Aralik = &RandomAralik;

    because

    char* (Aralik)(struct RASTGELEKARAKTER);

    but

    char* RandomAralik(RastgeleKarakter,unsigned,char,char);

    RandomAralik gets 4 arguments but Aralik must receive a pointer to a function getting only a struct RASTGELEKARAKTER*


    warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] karakter->VerilenlerArasindan = &RandomVerilenlerArasindan;

    because

    char* (VerilenlerArasindan)(struct RASTGELEKARAKTER);

    but

    char* RandomVerilenlerArasindan(RastgeleKarakter,unsigned,char*);

    RandomVerilenlerArasindan gets 3 arguments but VerilenlerArasindan must receive a pointer to a function getting only a struct RASTGELEKARAKTER*

  • answered 2019-04-21 10:22 Eric Postpischil

    The Harfler member is declared to be a pointer to a function that takes a struct RASTGELEKARAKTER *:

    char* (*Harfler)(struct RASTGELEKARAKTER*);
    

    but you attempt to assign to it a pointer to RandomHarf:

    karakter->Harf = &RandomHarf;
    

    which is declared to be a pointer to a function that takes a struct RASTGELEKARKTER * (via the typedef RastgeleKarakter) and an unsigned:

    char* RandomHarfler(RastgeleKarakter,unsigned);
    

    A pointer to a function that takes one parameter is not compatible with a pointer to a function that takes two parameters.