0

I found somewhere this weird variable declaration -

float (*x[20])(int *a); 

What could it possibly mean?
What is the purpose of it?

Farhan Hasin
  • 55
  • 1
  • 6
  • 2
    It's a dumber way of writing `typedef float func_t (int* a);` then declare `func_t* array[20];` – Lundin Jan 27 '16 at 07:22

4 Answers4

12

float (*x[20])(*int a) is not correct. It should be float (*x[20])(int *a) which declares x as an array of 20 pointers to a function that takes an argument of int * type and returns float.


For those who are curious to know the use of an array of function pointers:

typedef double Func(double, double);          // Declare a double (double, double)

Func sum, subtract, mul, divide;              // Function prototypes.
Func *p[] = { sum, subtract, mul, divide };   // Array of function pointers

int main(void)
{
    double result;
    double a, b;
    int option;
    printf("This is a simple calculator to add, subtract, multiply and divideide two integers\n"); 
    printf("Enter two integers: ");
    scanf("%lf %lf", &a, &b);

    printf("Choose an option:\n 1. Add\n 2. Subtract\n 3. Mult\n 4. Divide\n");
    scanf("%d", &option);

    result = p[option - 1](a, b); 
    printf("result = %lf\n", result);
}

double sum(double a, double b) { return a+b; }
double subtract(double a, double b) { return a-b; }
double mul(double a, double b) { return a*b; }
double divide(double a, double b) { return a/b; }

A detailed explanation on how to read/decipher such complex declaration is discussed here.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • is there any reason one should do that? I dont see anything this could be useful for. – Flikk Jan 27 '16 at 06:44
  • @Flikk; I added one simple use of array of function pointers. – haccks Jan 27 '16 at 07:11
  • @haccks Thx, that makes sense. i haven't worked with function pointers so far. How would it look like with the syntax from the question though? I guess something like `double (*p[ ])(double a, double b) = { sum, subtract, mul, divide };`. What else would this syntax need? The function prototypes? and anything else? – Flikk Jan 27 '16 at 07:49
  • @Flikk; Smarter way to write `double (*p[ ])(double a, double b) = { sum, subtract, mul, divide };` is `typedef double Func(double, double); Func *p[] = { sum, subtract, mul, divide };` – haccks Jan 27 '16 at 07:52
  • @haccks so if i don't have the typedef, i have to declare the prototypes like `double sum(double, double); double subtract(double, double); double mul(double, double); double divide(double, double);` or is there any way to write it shorter without the typedef? – Flikk Jan 27 '16 at 08:03
  • @Flikk; Yes. Having typedef, shorter version is `Func sum, subtract, mul, divide;` – haccks Jan 27 '16 at 16:01
  • Any reason why you have `main` returning a `double`? – dbush Aug 06 '18 at 00:08
  • @dbush actually I don't remember if I did that intentionally or accidentally. BTW, fixed that now. – haccks Aug 06 '18 at 00:32
1

this is an array of function pointers. it has 20 function pointer items.

linluk
  • 1,650
  • 16
  • 33
1

The declaration

float (*x[20])(int *a);

defines the variable x as an array of 20 functions (function pointers). In my humble opinion it's more clearly written as

Function x[20];

with

typedef float (*Function)(int *a);

The purpose of x is hard to tell without context, it could be to compute a statistical value like average, variance or standard deviation etc. given a set of integers and a function index input by the user:

x[0] = Average;
x[1] = Variance;
x[2] = StandardDeviation;
...

int a[100];
int i;

/*read data into `a' and function index into i...*/

printf("%f\n", x[i](a));
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
0

The code should be like this: - float (*x[20])(int *a); - as (*int a) seems to be incorrect. The code tells me that x is an array of 20 pointers to function that each takes an argument with datatype int and return float.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Ebram
  • 161
  • 1
  • 6