Shraddha said:
What is this exactly...
int(*(*ptr (int))(void)
First I thought that this is the pointer to function...But I recognize
that the syntax iss quite different...
If we say that the function is taking void parameters i.e. no
parameters then what "(int i)" is doing there...
Your parentheses are not balanced:
There are three cases to rebalance them:
1) you mean: int(*(*xxx ) (int)) (void) ;
2) you mean: int(*(*yyy (int)) ) (void) ;
3) you mean: int(*(*zzz (int)) (void) );
=====
1) you mean: int(*(*xxx ) (int)) (void) ;
xxx is pointer to function taking parameter int and returning...
a pointer to function taking parameter void and returning int.
static int count= 0;
int dec_count(void) { return count--; }
int inc_count(void) { return count++; }
typedef int (* count_func) (void);
count_func selector(int k) { return k ? inc_count : dec_count; }
you can then make xxx point to function selector:
int(*(*xxx ) (int)) (void)= selector;
or...
count_func (*xxx) (int)= selector;
=====
2) you mean: int(*(*yyy (int)) ) (void) ;
this is the same as:
int (** yyy (int)) (void);
yyy is a function taking parameter int and returning...
pointer to pointer to function taking parameter void and returning int.
int two (void) { return 2; }
int one (void) { return 1; }
typedef int (* func) (void);
func one_bis= one, two_bis= two;
then yyy can be defined as:
int (** yyy (int k)) (void) { return k ? & two_bis : & one_bis; }
or in a more readable way:
func * yyy (int choice) { return k ? & two_bis : & one_bis; }
=====
3) you mean: int(*(*zzz (int)) (void) );
this is the same as
int* (*zzz (int)) (void);
zzz is a function taking parameter int and returning...
pointer to function taking parameter void and returning int*.
int* null (void) { return NULL; }
int* nonnull (void) { static int hell= 666; return & hell; }
typedef int* (* func) (void);
then zzz can be defined as:
int* (*zzz (int)) (void) { return k ? nonnull : null; }
or in a more readable way:
func zzz (int k) { return k ? nonnull : null; }