c++ - Calling function via pointer -


here simple code

#include<iostream.h> #include<stdlib.h> #include<conio.h> void (*func[2])(int); void main(int n=1) {     int i;     cout<<endl<<n;     func[0]=&exit;     func[1]=&main;     i=++n<=10;     (func[i])(n); } 

here satisfied output (i.e. 1 10 in different lines). thing confused me why global pointer of type void (*ptr[2])(int). if possible, please explain in simple words why pointer taken specifically

it's not pointer, it's array of 2 pointers.

this function:

void func(int); 

this pointer function:

void (*func)(int); 

and array of 2 pointers functions:

void (*func[2])(int); 

so func[i] points exit if i 0 (i.e. if n greater 10), , points main otherwise, i 1.

note you're not allowed call main recursively this, nor give main signature other int main() or int main(int, char**). (at least, that's case in modern c++; these rules presumably don't apply prehistoric dialect compiler accepts).


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -