3

When I initialize function pointers in one take, like below, it does not work.

ptr[3]={add, subtract, multiply};

This gives:

[Error] expected expression before '{' token

However, one-by-one initialization works. Why is this?

//array of function pointers

#include<stdio.h>

void add(int a, int b){
    printf("%d\n", a+b);
}


void subtract(int a, int b){
    printf("%d\n", a-b);
}


void multiply(int a, int b){
    printf("%d\n", a*b);
}


int main(){
    
    void (*ptr[3])(int, int);
    
    
    //ptr[3]={add, subtract, multiply};  this initialization does not work
    
    //but this works
    ptr[0]=add;
    ptr[1]=subtract;
    ptr[2]=multiply;
    
    ptr[2](3,5); //15
    
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Lyrk
  • 1,936
  • 4
  • 26
  • 48
  • 2
    `ptr[3]` is an out of bounds access. You then try to assign a list to it, while `ptr[i]` is a single pointer. This is not initialization (because you do it separately from the declaration), and has nothing to do with function pointers (you'd get the same error with an `int` array). – HolyBlackCat Jan 06 '23 at 07:16
  • 1
    `void (*ptr[3])(int, int) = {add, subtract, multiply};` initialization like this should work – Gaurav Pathak Jan 06 '23 at 07:17
  • can I make declaration and assignment in separate lines for function pointers? @HolyBlackCat – Lyrk Jan 06 '23 at 07:26
  • 1
    Only the way you did it, by separately assigning to each element. – HolyBlackCat Jan 06 '23 at 07:33

2 Answers2

2

In the assignment, ptr[3]={add, subtract, multiply}; the RHS is (correctly) a suitable initializer-list for an array of three function pointers. However, the LHS (ptr[3]) is wrong: that's just a single element of an array, and an out-of-bounds element, at that.

Just do the 'assignment' in the declaration, and make it an initialisation:

int main(void)
{
    void (*ptr[3])(int, int) = {add, subtract, multiply}; // this initialization does work
    ptr[2](3, 5); //15
}

There is actually nothing special, here, related to the fact that your array's elements are function pointers. No array can be "assigned to" (using the = operator) en bloc, at any point other than in its declaration. In a variable declaration, the use of the = token isn't, formally, an assignment operation; it is an initialisation. Useful reading: Initialization vs Assignment in C.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • but when I initialize an array of ints, I can do int arr[3]={4,6,7}; I mean this is not the 3rd element, I am declaring an array of 3 integers. I am not trying to assign the 3rd element. – Lyrk Jan 06 '23 at 07:23
  • 1
    @Lyrk Yes, but you can't do `int arr[3];` and then, in a **separate statement**, do `arr[3] = {4,6,7};`. In fact, arrays can ***only*** be intialised with such lists at the point of their declaration. – Adrian Mole Jan 06 '23 at 07:24
1

You need to initialize during declaration.

//array of function pointers

#include<stdio.h>

void add(int a, int b){
    printf("%d\n", a+b);
}


void subtract(int a, int b){
    printf("%d\n", a-b);
}


void multiply(int a, int b){
    printf("%d\n", a*b);
}


int main(){
    
    void (*ptr[3])(int, int) = {add, subtract, multiply};
    
    ptr[2](4,5); //20
    
}
Nitin Jadhav
  • 497
  • 1
  • 7
  • 17
  • "You need to initialize during declaration." Is this a rule for function pointers only? because I can do declaration and assignment on separate lines in other pointer variables. – Lyrk Jan 06 '23 at 07:25
  • 1
    @Lyrk On a separate line that's an assignment, not initialization. The difference matters primarily for arrays, which can't be assigned as a whole in a single line. – HolyBlackCat Jan 06 '23 at 07:34
  • 1
    thank you so much. C is overwhelming. @HolyBlackCat – Lyrk Jan 06 '23 at 07:40
  • 1
    @Lyrk Wait until you get to C++. D: – HolyBlackCat Jan 06 '23 at 07:41