Search code examples
arrayscfunctioncallfill

fill an array of pointers to functions in another function in C


I tried to fill my array of pointer to functions in another function but I don't know how I can do it.

Below my function to fill the array

void* fill_tab_f(void (***tab_f))
{

    *tab_f[0] = ft_pt_char;
    *tab_f[1] = ft_pt_str;
    *tab_f[2] = ft_pt_ptr;
    *tab_f[3] = ft_pt_int;
    *tab_f[4] = ft_pt_int;
    *tab_f[5] = ft_pt_un_int;
    *tab_f[6] = ft_pt_hexa_min;
    *tab_f[7] = ft_pt_hexa_maj;
    
    return NULL;
}

Below the declaration of the array of pointers to functions and the call of the function to fill my array.

void(*tab_f[8])(va_list *, Variable*);
fill_tab_f(&tab_f);

Thanks for your answers.


Solution

  • You are overcomplicating it

    typedef int variable ;
    
    void fill_tab_f(void (*tab_f[])(va_list *, variable *))
    {
    
        tab_f[0] = ft_pt_char;
        tab_f[1] = ft_pt_str;
        tab_f[2] = ft_pt_ptr;
        tab_f[3] = ft_pt_int;
        tab_f[4] = ft_pt_int;
        tab_f[5] = ft_pt_un_int;
        tab_f[6] = ft_pt_hexa_min;
        tab_f[7] = ft_pt_hexa_maj;
    }
    

    or if function pointer syntax is a bit weird for you:

    typedef int variable ;
    
    typedef void (*funcptr)(va_list *, variable *);
    
    void fill_tab_f(funcptr *tab_f)
    {
    
        tab_f[0] = ft_pt_char;
        tab_f[1] = ft_pt_str;
        tab_f[2] = ft_pt_ptr;
        tab_f[3] = ft_pt_int;
        tab_f[4] = ft_pt_int;
        tab_f[5] = ft_pt_un_int;
        tab_f[6] = ft_pt_hexa_min;
        tab_f[7] = ft_pt_hexa_maj;
    }