Search code examples
cfunctionpointersfunction-pointers

Function pointer code not executing properly


i am trying to learn function pointers and i just wanted to make 3 functions that have pointers to them which are used in other 2 functions which i am calling through function pointer arrays. The code doesn't give any errors, but i receive addresses instead of values. I tried dereferencing and such, but nothing changes. I am sure i am overlooking something simple, but i am not sure what to do, or where my mistake lies.

The explicit casting is there, because i was just playing around with it, no particular reason.

#include <stdio.h>

#include <stdlib.h>

unsigned int par1(void)
{
    return 5;
}

unsigned int par2(void)
{
    return 6;
}

unsigned int par3(void)
{
    return 7;
}

const unsigned int x = 10;


typedef unsigned int (*parPtr)(void);
parPtr parP1 = &par1;
parPtr parP2 = &par2;
parPtr parP3 = &par3;

/** typedef for sum() and scad() */
typedef int (*sumscadPtr)(parPtr, parPtr);

sumscadPtr sumscadPtrArr[2];

int sum(parPtr, parPtr);

int scad(parPtr, const unsigned int);


void allocate_array_of_pointers(void)
{

    sumscadPtrArr[0] = &sum;
    sumscadPtrArr[1] = &scad;


}


// should return 11
int sum(parPtr parA, parPtr parB)
{
    unsigned int value1 = *parA;
    printf("\n%d\n", value1);
    unsigned int value2 = *parB;
    printf("\n%d\n", value2);

    return (int)value1 + (int)value2;
}


// should return 3
int scad(parPtr parA, unsigned const int b)
{
    unsigned int value1 = parA;

    // This is just a simple check in case i might change the const value in the future
    if((int)value1 > (int)b)
        return (int)value1 - (int)b;
    else
        return (int)b - (int)value1;
}




int main()
{
    allocate_array_of_pointers();

    int sumVal = sumscadPtrArr[0](&par1, &par2);
    int scadVal = sumscadPtrArr[1](&par3, x);

    printf("\nsumVal is :%d\n", sumVal);
    printf("\nscadVal is :%d\n", scadVal);

    int a = *parP1;
    printf("\n%d\n", a);


    return 0;
}

Solution

  • Your code failed to compile with these errors:

    main.cpp:42:24: error: invalid conversion from 'int (*)(parPtr, unsigned int)' {aka 'int (*)(unsigned int (*)(), unsigned int)'} to 'sumscadPtr' {aka 'int (*)(unsigned int (*)(), unsigned int (*)())'} [-fpermissive]
       42 |     sumscadPtrArr[1] = &scad;
    

    which is quite obvious since your're storing the pointer to a function with a different function signature in sumscadPtrArr[1] other than parPtr.

    and then:

    error: invalid conversion from 'parPtr' {aka 'unsigned int (*)()'} to 'unsigned int' [-fpermissive]
       51 |     unsigned int value1 = *parA;
    

    and similar errors for all such invocations of applying the * to parPtr which is for assigning a parPtr to unsigned int. The correct way would be

    unsigned int value1 = parA();
    

    Function pointers cane be dereferenced in a couple of ways:

    1. (*fnPtr)()
    2. fnPtr()

    The function pointer* array/table that you are attempting to create with sumscadPtrArr should ideally contain functions of the same type or you possibly could create a struct with something like:

    struct fPtrTablle {
        int (*sumFn)(parPtr, parPtr);
        int (*scadFn)(parPtr, const unsigned int);
    };
    
    struct fPtrTablle FnTable = { sum, scad};
    
    void initFnTable(void)
    {
        FnTable.sumFn =  &sum;
        FnTable.scadFn = &scad;
    }
    

    and then call it from main() as:

    initFnTable();
    
    int sumVal = FnTable.sumFn(&par1, &par2);
    int scadVal = FnTable.scadFn(&par3, x);
    

    Complete compilable program here.