Functions in Arrays?

Started by
6 comments, last by caffeineaddict 22 years, 8 months ago
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
Advertisement
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 bracketsint 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
[email=gil@gilzu.com]Gil Zussman[/email]Check out Goose Chase, Coming this fall!http://www.gilzu.com
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
For me, using VC++, I had to do this, or it wouldnt compile:

f=&func

//No brackets

“If you try and please everyone, you won’t please anyone.”
the address of a function can be retrieved by just leaving the brackets away...

thus

f = &func();
or maybe safer
f = &(func());

would be simply
f = func;

does
f = &func
work also?
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 )();
...
}





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.
Thanks for the replies. Dean Harding, sorry about that, I knew I said a 2x2 array, but then accidently filled in as a 3x3. Well i''ll try those things out and see if it works. Thanks again.

This topic is closed to new replies.

Advertisement