If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: how to properly use a function pointer to a particular function

Re: how to properly use a function pointer to a particular function

When you post code, please format and indent it properly first and use code tags (Go Advanced, select code and click '#') so that the code is readable. Also, it would be useful if you actually asked a specific question!

As you haven't asked a specific question, I'm assuming you are referring to the compiler error regarding undeclared identifiers in the line

Code:

if ((compare)(c, (*cmp)(a, b))) {

This is because a and b haven't been declared!

Looking at the function definiton, you have

Code:

void function3(int c, int(*cmp)(int a,int b) )

This function takes 2 parameters. The first is an int, the second is a function that takes as arguments 2 ints and returns an int. The names of these arguments is ignored here by the compiler (you don't even need to provide them - the same way as you don't need to provide argument names in function declarations). If you want to pass two ints to function3 as well then the function definition would be

Code:

void function3(int c, int(*cmp)(int, int), int a, int b)

When dealing with function pointers, I like to use typedefs to define a function type.

Code:

typedef int (*IFUNCII)(int, int);

Which defines a type IFUNCII as a function pointer for functions that take 2 int parameters and return an int. Using this, the function3 definition would be

Code:

void function3(int c, IFUNCII cmp, int a, int b)

An example of using it is

Code:

function3(12, function, 5, 10);

I hope this helps.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.