can i make a function array with pointers?

Understanding the language, error messages, etc.

can i make a function array with pointers?

Postby REGFly » Wed Jun 24, 2015 9:43 pm

The question is pretty straightforward. I recently came back from a huge break because of exams and now i'm resuming my snake game, but i can't remember how to do this, if i ever even knew how to do this :lol:
This is what i wrote:
Code: Select all

int const * menuChoice[] =
{

singleGame(),
multiGame(),
classicGame(),
changeSettings(),
gameCredits(),
terminate(),
 
};



but it dosen't work
REGFly
 
Posts: 62
Joined: Sat Mar 07, 2015 10:21 pm

Re: can i make a function array with pointers?

Postby awesome101 » Wed Jun 24, 2015 10:02 pm

Yeah you can. This is some code for a simple c++ program.

#include <iostream>
using namespace std;

typedef int (*IntFunctionWithOneParameter) (int a);

int function(int a){ return a; }
int functionTimesTwo(int a){ return a*2; }
int functionDivideByTwo(int a){ return a/2; }

void main()
{
IntFunctionWithOneParameter functions[] =
{
function,
functionTimesTwo,
functionDivideByTwo
};

for(int i = 0; i < 3; ++i)
{
cout << functions[i](8) << endl;
}
}
awesome101
 
Posts: 159
Joined: Sat Jun 20, 2015 6:56 pm

Re: can i make a function array with pointers?

Postby REGFly » Thu Jun 25, 2015 1:27 pm

And if my functions dont take anything or return anything could i declare it like this?
Typedef void (*arrayFunction);
REGFly
 
Posts: 62
Joined: Sat Mar 07, 2015 10:21 pm

Re: can i make a function array with pointers?

Postby Myndale » Sun Jul 19, 2015 6:56 am

Close:

Code: Select all
typedef void (*arrayFunction)();
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 71 guests

cron