DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   pointers to function (http://www.daniweb.com/forums/thread156774.html)

hellIon Nov 12th, 2008 3:32 am
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

Sci@phy Nov 12th, 2008 4:25 am
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.

hellIon Nov 12th, 2008 5:23 am
Re: pointers to function
 
Quote:

Originally Posted by Sci@phy (Post 733953)
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......

Narue Nov 12th, 2008 10:06 am
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++.

hellIon Nov 12th, 2008 12:59 pm
Re: pointers to function
 
Quote:

Originally Posted by Narue (Post 734152)
>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.....

Narue Nov 12th, 2008 1:04 pm
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.


All times are GMT -4. The time now is 1:26 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC