Problem analiza statyczna, funkcja zwracjaca wskaznik do funkcji

0

Cześć szukam pomocy , przeskanowalem kod CPPcheckiem
wywala mi warning :

portability Returning an integer in a function with pointer return type is not portable.

int (*wsk(enum operations op))(int,int)
{
    int (*funcs[4])(int,int)=
    {
        add, 
        sub, 
        div, 
        mul
    };
    
    
    if(op==op_add)
    {
        return *(funcs + 0);
    }else if(op==op_sub)
    {
        return *(funcs + 1);
    }else if(op==op_div)
    {
        return *(funcs + 2);
    }else if(op==op_mul)
    {
        return *(funcs + 3);
    }else
    {
        return NULL;
    }
    
}



1

Strasznie przekombinowałeś ten kod.
Strasznie się to czyta. Kod przy którym trzeba wytężać wzrok, by go zrozumieć to kod będzie kosztował czas wszystkich przyszłych czytelników i będzie wabił bugi.
Po poprawkach jest to bardziej strawne i po prostu czytelne:

typedef int (*OperationPtr)(int, int);

int no_operation(int, int)
{
    assert(0);
    return 0;
}

OperationPtr wsk(enum operations op) 
{
    static const OperationPtr funcs[4] = {
        add, 
        sub, 
        div, 
        mul
    };

    if (op >= op_add && op <= op_mul) {
        return funcs[op];
    }
    return NULL; // a po naprawie return no_operation;
}

teraz widać, że problemem dla cppcheck jest NULL, które w c zwykle jest po prostu zerem.
W pewnym sensie jest to false positive, ale ja bym użył funkcji no_operation na taki przypadek.

1 użytkowników online, w tym zalogowanych: 0, gości: 1