I don't tend to use Arrays when programming in C++ because I think that vectors are a lot easier to use.

The problem is that now I have to use arrays. I'm creating a game using SDL. I'm trying to create the artwork in code using pixels on the screen. So all the artwork information is stored in Multidimensional Arrays. I'm trying to create a function that will draw the bitmap on the screen but the problem is that I dont know how to pass a multidimensional array as a parameter to the function. I know the size of the bitmap that I'm passing to the function but I'm not sure how to initialise the parameters.

This is an example of the code - fail to compile

#include <iostream>

int bitmap[5][5] = {
{0,0,1,0,0},
{1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1},
{0,0,1,0,0}
};



void DrawBitmap(int arr[0][0], int w, int h)
{
	for(int i=0;i<w;i++)
	{
		for(int j=0;j<h;j++)
		{
			if(arr[w][h] == 1)
			{
				// draw pixel on the screen
			}
				
		}
	}
}

int main (int argc, char * const argv[]) 
{

	DrawBitmap(bitmap, 5, 5);
		
	    return 0;
}

You have your array declare globally, I'd change that.

Also, try declaring the range of the array inside your function. I'd imagine by the nature of multidimensional arrays your compiler will want that specified.

Example:

myfunc(int array[0][5]) //change the '0' to '5'.

Thanks! The problem is that the size will change with every different array that I'm sending to the function so I have to find another way to initialise it...

This article has been dead for over six months. Start a new discussion instead.