0

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.

3
  • 4
    Why can't you do it? Does it fail to compile? Is there an error message? Does it just crash when you try to use it? Commented Jul 9, 2021 at 14:43
  • 1
    Having a triple-asterisk in the parameter, but then dereferencing your array members, seems a bit odd. Why not just assign the function pointers directly? Commented Jul 9, 2021 at 14:45
  • Instinctively, it feels to me like you should be using array initialization here, not a runtime function. This can all be done at compile time, unless your intent is to change out these functions at will during runtime. See here for more details. Commented Jul 9, 2021 at 14:50

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@EricPostpischil does it matter considering the question?
@ChrisDodd well spotted. Too long week at work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.