943,600 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 768
  • C RSS
Nov 12th, 2008
0

pointers to function

Expand Post »
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....

  1.  
  2.  
  3. #include <stdio.h>
  4.  
  5. int sum(int x, int y)
  6. {
  7. return x + y;
  8. }
  9.  
  10. int product(int x, int y)
  11. {
  12. return x * y;
  13. }
  14.  
  15.  
  16. int main()
  17. {
  18. int a = 13;
  19. int b = 5;
  20. int result = 0;
  21. int (*pfun)(int, int);
  22.  
  23. pfun = sum;
  24. result = pfun(a, b);
  25. printf("\npfun = sum result = %d", result);
  26.  
  27. pfun = product;
  28. result = pfun(a, b);
  29. printf("\npfun = product result = %d", result);
  30.  
  31. }
  32. //this source code is from
  33. //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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hellIon is offline Offline
18 posts
since Nov 2008
Nov 12th, 2008
0

Re: pointers to function

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.
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Nov 12th, 2008
0

Re: pointers to function

Click to Expand / Collapse  Quote originally posted by Sci@phy ...
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......
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hellIon is offline Offline
18 posts
since Nov 2008
Nov 12th, 2008
1

Re: pointers to function

>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++.
Last edited by Narue; Nov 12th, 2008 at 10:07 am.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 12th, 2008
0

Re: pointers to function

Click to Expand / Collapse  Quote originally posted by Narue ...
>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.....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hellIon is offline Offline
18 posts
since Nov 2008
Nov 12th, 2008
1

Re: pointers to function

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: help me in array
Next Thread in C Forum Timeline: Reading ints from a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC