Hi,
I'm reading one article about function pointers and I found this simple code:
I read there about "correct ways" to call function through function pointer.Code:#include <stdio.h> typedef int (*pFunc) (int, int); int sum (int, int); int diff (int, int); int main ( void ) { pFunc pfArr[2]; pfArr[0] = ∑ /* correct way*/ /*pfArr[0] = sum; Possible incorrect way ???*/ pfArr[1] = &diff; printf("%d",pfArr[0](2,3)); printf("%d",(*pfArr[0])(2,3)); /*correct way*/ return 0; } int sum (int a, int b) { return a + b; } int diff (int a, int b) { return a - b; }
I tried both solutions and both works fine.
What I'm not sure is whether both solutions are valid according to the Standard. Please, can you comment thisCode:printf("%d",pfArr[0](2,3)); printf("%d",(*pfArr[0])(2,3)); /*correct way*/
Thanks
- Micko



LinkBack URL
About LinkBacks


