954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

pointers to function

hey guys
i am using miracle c compiler on windows xp....
i am trying to figure out how does pointer to functions work.....
i have gone thru many tutorials but all have them failed to compile....

#include <stdio.h>

int sum(int x, int y)
{
   return x + y;
}

int product(int x, int y)
{
   return x * y;
}


int main()
{
   int a = 13;
   int b = 5;
   int result = 0;
   int (*pfun)(int, int);

   pfun = sum;
   result = pfun(a, b);
   printf("\npfun = sum   result = %d", result);

   pfun = product;
   result = pfun(a, b);
   printf("\npfun = product         result = %d", result);

}
//this source code is from
//http://www.java2s.com/Code/C/Function/Pointingtofunctions.htm

i get this error.......

C:\Program Files\Miracle C\work.c: line 21: Parse Error
'int (*pfun)(int, int)'
aborting compile

pls tell me wats wrong and also send me source code explaining the pointers to function

hellIon
Newbie Poster
18 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

It works on my Dev-cpp (mingw, gcc) compiler, and I can't see any problem with code, so it's probably something about your compiler.

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 
It works on my Dev-cpp (mingw, gcc) compiler, and I can't see any problem with code, so it's probably something about your compiler.

so wat compiler do u suggest......

hellIon
Newbie Poster
18 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

>i am using miracle c compiler
Miracle C is a horrible compiler. Trash it and get anything else, or you'll have to suffer through constant battles of perfectly valid code failing to compile or not working correctly.

Try Code::Blocks , Turbo C++ , or Visual C++ .

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>i am using miracle c compiler Miracle C is a horrible compiler. Trash it and get anything else, or you'll have to suffer through constant battles of perfectly valid code failing to compile or not working correctly.

Try Code::Blocks , Turbo C++ , or Visual C++ .


i am learning c
so how can i use visual c++,turbo c++......
pls can u suggest a compiler for windows.........
its urgent.....

hellIon
Newbie Poster
18 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

>i am learning c
C++ compilers can compile C. Usually all you have to do is name the source file *.c instead of *.cpp.

>pls can u suggest a compiler for windows.........
I suggested three of them. They're all easy to install and use. Stop being lazy.

>its urgent.....
Not for me.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You