Function pointers, arrays and values ​​in C

Suppose we have the following functions (in C):

int sum(int a, int b){ return a+b; } int diff(int a, int b){ return ab; } 

So we know that we can declare an array of funtion pointers as follows:

 int (*test[2]) (int a, int b); test[0] = sum; test[1] = diff; 

But the following is also true (but we use heap allocation):

 int (**test) (int a, int b) = malloc( 2*sizeof(*test)); test[0] = sum; test[1] = diff; 

So far so good. Now recall that to declare an (dynamically allocated) array of two integers, we can do:

  int* test = malloc( 2*sizeof(int)); 

So why can't we declare an array of function pointers as

 int (*test) (int a, int b) = malloc( 2*sizeof(*test)); ? 

Is it the reason that the test is the same as *test and **test (etc.), malloc( 2*sizeof(*test)) returns a pointer to a function pointer and therefore cannot be assigned (*test)

If this assumption is correct, you can explain in detail why we get a compilation error

 error: lvalue required as left operand of assignment 

when we try to do

 int (*test) (int a, int b) = malloc( 2*sizeof(*test)); test=diff; //<--- This is ok. test+1 = sum; //<--- This is what gives the error! 

Disclaimer: I believe this is a basic question and the assumption is true, but I would like a more detailed explanation so that this view is understandable to everyone.


Edit:

Please note that this is equivalent

 int (*test) (int a, int b) = malloc( 2*sizeof(*test)); *test=*diff; //<--- This is ok. *(test+1) = *sum; //<--- This is what gives the error! 

as this is somewhat more like a case:

 int *test = malloc(2*sizeof(*test)); *test = 0; *(test+1) = 1; 
+7
c arrays pointers
source share
1 answer

So why can't we declare an array of function pointers as

 int (*test) (int a, int b) = malloc( 2*sizeof(*test)); 

Because test does not point to a function pointer; This is a pointer to a function. Thus, it cannot point to the first element of an array of function pointers.

If you want the array of function pointers to use the previous form:

 int (**test) (int a, int b) = malloc( 2*sizeof(*test)); 

Here *test has the type of a function pointer and, therefore, test can (and does) point to the first element of the array of function pointers. Further:

 error: lvalue required as left operand of assignment 

when we try to do

  int (*test) (int a, int b) = malloc( 2*sizeof(*test)); test=diff; //<--- This is ok. test+1 = sum; //<--- This is what gives the error! 

Regardless of type test , test+1=anything always invalid C. test+1 never be lvalue. I do not understand why you expect this to work.

GCC also documents another error in your program, sizeof(*test) . Since *test has a function type, sizeof(*test) is not valid, but GCC silently sets it to 1. This causes too little memory to be allocated for the function pointer, but it does not matter, because you throw out memory in the next line obtained from malloc and assign something else to test .

+11
source share

All Articles