Jump to content
  • Advertisement
Sign in to follow this  

Trouble with passing an SDL_Rect array to a function

This topic is 2218 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

I'm having trouble understanding passing an array to a function. I've been looking on google and I read an article on pointers but I still just don't get this part, so I'm hoping you all can help.

What I have is 2 classes for my Tetris game, a block class and a board class.
The block class has an array SDL_Rect blockBoxes[4]; which holds the collisions boxes of each block in a shape.
I'm wanting to pass this to one of the board class's functions, for example:

Board::set_board(SDL_Rect array[]) { }; // the functions

Board game_field(); // the object


How can I accomplish this and access the members of the array within the function?

Any advice, suggestions, or links to articles are much appreciated.
This is my first SDL game, and 2nd game total. I usually just give up on this topic and just make things I need global but I want to finally learn and understand this while Im still practicing. Thank you again! Edited by ChainedHollow

Share this post

Link to post
Share on other sites
Maybe you should consider using vectors instead of arrays. Consider this example:

class Board
void set_board(vector<SDL_Rect> boxes)
for(int i = 0; i < boxes.size(); ++i)
cout << boxes.x << endl;
cout << boxes.y << endl;
cout << boxes.w << endl;
cout << boxes.h << endl;
int main()
Board game_field;
SDL_Rect sr1;
sr1.x = 10;
sr1.y = 20;
sr1.w = 30;
sr1.h = 40;
SDL_Rect sr2;
sr2.x = 50;
sr2.y = 60;
sr2.w = 70;
sr2.h = 80;
vector<SDL_Rect> blockBoxes;
return 0;

This prints out:


By using a vector, you have the advantage of a dynamic-length container and it also remembers its size, making it easy to pass data around. Edited by kidman171

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • 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!