Archived

This topic is now archived and is closed to further replies.

caffeineaddict

Functions in Arrays?

Recommended Posts

Hi i''m writing a new program/small game and I had an idea about something but i''m not sure if it would work. I''ll try to explain. I want to make things in a 2 dimensional array be generated at run time, maybe use a 3 dimensional array to not sure though, anyway, i wanted to write a function that did all the necessary stuff to fill in the array and then put the function in the array to put the information there. For example int function() { do stuff... } int array[2][2] { function();,function();,function();, function();,function();,function();, function();,function();,function();, }; would this work? is there another, better and or efficient way to do this, i''m open to criticism, i''m not a genius at this stuff but i''m trying to learn

Share this post


Link to post
Share on other sites
it is possible, by making the array as a pointer array to
functions, as described:

  
int (*f)(); //declaration of a pointer to a function

//you can use this to put an array by adding brackets


int func()
{
//your code here

};

void main()
{
f=&func();
//now we''ll just call the function

(*f)();
}

/now we can call the function as



anyway, it works fine for me

Share this post


Link to post
Share on other sites
Is an array of function pointers really what you need?

In particular, why exactly do you need a 2-d array as opposed to a 1-D array?

Just curious. 8)

Timkin

Share this post


Link to post
Share on other sites
well, you can use something like this :
Works fine with my source code (VC++ v6.0)

typedef HRESULT (* tagRenderFunc)();
tagRenderFunc RenderFunc[3];

HRESULT Func1();
HRESULT Func2();

enum eGameState
{
eStatus1,
eStatus2,
} g_GameState;

HRESULT InitGame()
{
...
RenderFunc[0] = Func1;
RenderFunc[1] = Func2; // etc
.....

}


HRESULT RenderGame()
{
....
RenderFunc( (long)g_GameState )();
...
}





Share this post


Link to post
Share on other sites
Actually, I think what he''s looking for is to fill an array using a function, not an array of function pointers. Is that right?

To do that, it''s quite simple, just go like this:

  
int array[2][2];
for( int i = 0; i < 2; i++ )
{
for( int j = 0; j < 2; j++ )
{
array[i][j] = function();
}
}


What you''ve got won''t compile, because a static array initializer has to be filled with static data. Also, the size of your array and the size of the static initializer are different (you''ve said the array is 2x2, but you''ve given a 3x3 initializer).

Finally, to LeMatsch, your examples of f = &func(); and f = &(func()); aren''t legal. If you do &(func()), then the compiler will first evaluate (i.e. call the function) func(), and then try and take the address of what it returns. While that''s nothing at all what you''re trying to do, it''s also not valid (since you can''t get the address of a returned value).


War Worlds - A 3D Real-Time Strategy game in development.

Share this post


Link to post
Share on other sites