Jump to content
  • Advertisement

Archived

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

caffeineaddict

Functions in Arrays?

This topic is 6203 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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 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
For me, using VC++, I had to do this, or it wouldnt compile:

f=&func;

//No brackets

Share this post


Link to post
Share on other sites
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?

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
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!