Why is my array not being passed correctly?

I have a 2D array which I'm trying to pass to another function but when I go to pass it, the function which I'm passing it to only gets one "row" of the array. Here's the simplified version of my program:

1
2
3
4
5
6
7
8
9
10
11
int main(){
int numberMatrix[3][3];

fillMatrix(); //this function fills my matrix
passMatrix(numberMatrix);
}

passMatrix(int numberMatrix[MATRIX_SIZE][MATRIX_SIZE]){

}
Just change the declaration in ur functions

1
2
3
4
passMatrix(int *numberMatrix)
{

}


A Matrix is a pointer pointing to 1st position ( [0][0] ) if left without the [ ] ;) Actually even with the [] [] its a pointer always of course pointing to the ++ positions but its more complicated and it will difficult you so leave this thought for now...

Otherwise if you dont want to change the function declaration of matrix think you have to pass it as an antire matrix, i think like

passMatrix(numberMatrix [] [] );

or

passMatrix(numberMatrix [3] [3]);
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
void myFunc( int ary[][ 2 ] )
{
	// do stuff
}

int main()
{
	int ary[ 2 ][ 2 ];

	myFunc( ary );
    
    return 0;
}
Thanks for the replies! I still can't seem to get it to work though. I'm going to post my code a bit more detailed to see if someone can hopefully (please) help me out. Here's the important parts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool isMagic(const int matrixArray[3][3]);

int main(){
int numberMatrix[3][3];

fillMatrix(string numbers); // this method fills my matrix by converting a string into ints and
                                         // placing the ints into a 2D array. The matrix is filled the way
                                         // I want it to be at this point.

isMagic(numberMatrix);    // When I try calling this function and passing the matrix, the matrix 
                                         // doesn't get sent in full, only it's first row.

}

bool isMagic(const int numberMatrix[3][3]){
// This is where I need to use the matrix, but for some reason, only the first "row" is passed 
// into here

}
Last edited on
From the looks of your code, you're creating another 2D array in the function 'fillMatrix'?

To fill the numberMatrix in main, you'd have to pass the 2D array to the 'fillMatrix' function, as well as the string 'numbers'.

Did you try what I posted before?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// matrixArray[][ MAX_ELEMENT ]
bool isMagic(const int matrixArray[][3]);

int main(){
int numberMatrix[3][3];

fillMatrix(string numbers); // this method fills my matrix by converting a string into ints and
                                         // placing the ints into a 2D array. The matrix is filled the way
                                         // I want it to be at this point.

isMagic(numberMatrix);    // When I try calling this function and passing the matrix, the matrix 
                                         // doesn't get sent in full, only it's first row.

}

bool isMagic(const int numberMatrix[][3]){
// This is where I need to use the matrix, but for some reason, only the first "row" is passed 
// into here

}
Yeah, I tried that but it still only gives me the first row. In a matrix that looks like this:

[1][2][3]
[4][5][6]
[7][8][9]

I only get 1, 2 and 3 in my isMagic function. The 2D array that's passed in doesnt know about the other two rows for some reason.
Can you post your code from the isMagic function.
It doesn't really do anything yet....I cant do what I want to do with it because I can't get the array to get passed in correctly. The purpose of the function (once it works correctly) is to iterate through each element in the 2D array and ensure that each element has a unique value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool isMagic(const int matrixArray[][MATRIX_SIZE]);

int main(){
int numberMatrix[MATRIX_SIZE][MATRIX_SIZE];

bool isMagic = isMagic(numberMatrix);
}

bool isMagic(const int numberMatrix[][MATRIX_SIZE]){
	bool foundDuplicateNumber = false;
	  for(int i = 1; i <= MATRIX_SIZE * MATRIX_SIZE; i++){
		 for(int j = 0; j < MATRIX_SIZE; j++){
			 for(int k = 0; k < MATRIX_SIZE; k++){
				 // when instance of number is found
				   if(numberMatrix[j][k] == i){
					 break;
				 }
			 }		  
	     } 
	
	  // only one of each 

	return false;
}


Thanks for helping
Last edited on
Figured it out....It was my compiler (Visual stuio 2010) that wasn't displaying my array correctly. Since passing an array only passes a reference the the first index, it was showing me only the first index's row. I manually typed in the location of the other rows in my watch window and i saw the values.
Topic archived. No new replies allowed.