This is a syntax question. I stress that I would like to understand how to read the code below.
I am having enormous trouble trying to understand how the following code (1) translates to the code under it (2):
Code Zero:
int addInt(int n, int m) {
return n+m;
}
Code One:
// this is a function called functionFactory which receives parameter n
// and returns a pointer to another function which receives two ints
// and it returns another int
int (*functionFactory(int n))(int, int) {
printf("Got parameter %d", n);
int (*functionPtr)(int,int) = &addInt;
return functionPtr;
}
Code Two:
typedef int (*myFuncDef)(int, int);
// note that the typedef name is indeed myFuncDef
myFuncDef functionFactory(int n) {
printf("Got parameter %d", n);
myFuncDef functionPtr = &addInt;
return functionPtr;
}
I am struggling with two pieces and here is why. I have modified the code above to what I believe they SHOULD look like.
Explicit Function Definition Without Typedef (Should be identical to title:
Code 4:
int (*myFuncDef)(int, int) functionFactory(int n) {
printf("Got parameter %d", n);
int (*functionPtr)(int,int) = &addInt;
return functionPtr;
}
Code 5: The typedef itself (used to simplify in code 2):
typedef int (*myFuncDef)(int, int) myFuncDef;
Note that these prescribe to the basic rule: return type, idenitifer, parameters.
I would really appreciate a link to where I can read the rigorous rules of how this all works out. And an overview explanation would be great because the spec does not provide 'tutorial' like lessons. Thank you very much!
[EDIT] Also,
Note, these are excerpt from: How do function pointers in C work?