View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Functions in Arrays?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

Posted 22 July 2001 - 08:27 PM

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

#2Gilzu  Members

Posted 22 July 2001 - 08:45 PM

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

#3Timkin  Members

Posted 22 July 2001 - 09:01 PM

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

#4kingy  Members

Posted 22 July 2001 - 10:50 PM

For me, using VC++, I had to do this, or it wouldnt compile:

f=&func;

//No brackets

#5LeMatsch  Members

Posted 22 July 2001 - 11:11 PM

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?

#6kostasp  Members

Posted 22 July 2001 - 11:15 PM

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 )();
...
}

#7Dean Harding  Members

Posted 23 July 2001 - 01:54 AM

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.