Reputation: 12484
My professor showed us this code:
timerX(int x){
int times(int y){
return x * y;
}
return times;
}
How does this work in C(using GCC compiler)? He said that as soon as the function disappears the inside function disappears? I appreciate any tips or advice.
Upvotes: 5
Views: 96
Reputation: 67345
I'm pretty sure it works just like any other function, except that it is only visible to the enclosing function.
In other words, it's just related to the visibility or accessibility of the function, and nothing else.
Upvotes: 3
Reputation: 182744
It's called a nested function, a GNU extension. Basically
the inner function can acess the local variables of the outer function (the ones declared prior to its apparition)
the inner function can only be called from outside via function poinyers but not after the containing function has terminated if the inner function accesses objects from its parent
In your example, calling that function pointer from outside will probably be illegal.
If you try to call the nested function through its address after the containing function has exited, all hell will break loose.
Upvotes: 7