pointers to function

Reply

Join Date: Nov 2008
Posts: 18
Reputation: hellIon is an unknown quantity at this point 
Solved Threads: 0
hellIon hellIon is offline Offline
Newbie Poster

pointers to function

 
0
  #1
Nov 12th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: pointers to function

 
0
  #2
Nov 12th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 18
Reputation: hellIon is an unknown quantity at this point 
Solved Threads: 0
hellIon hellIon is offline Offline
Newbie Poster

Re: pointers to function

 
0
  #3
Nov 12th, 2008
Originally Posted by Sci@phy View Post
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......
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: pointers to function

 
0
  #4
Nov 12th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 18
Reputation: hellIon is an unknown quantity at this point 
Solved Threads: 0
hellIon hellIon is offline Offline
Newbie Poster

Re: pointers to function

 
0
  #5
Nov 12th, 2008
Originally Posted by Narue View Post
>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.....
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: pointers to function

 
0
  #6
Nov 12th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC