954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

passing matrix by ref

i have two func from one i am passing address of a matrix and in another function getting elemts in matrix. getting coredump on entering first value

fn1()
{
int aiMat[MAX_ROW][MAX_COL];
int iNoOfRows,iNoOfCol;
/* calls the function to get matrix from user*/
fnGetMatrix(aiMat,&iNoOfRows,&iNoOfCol);
............
..............

}

void fnGetMatrix(int **aiMat,int *iNoOfRows,int *iNoOfCol)
{
.......
.........

for(int iRowCount = 0; iRowCount < *iNoOfRows; iRowCount++)
{
for(int iColCount = 0; iColCount < *iNoOfCol; iColCount++)
{
scanf("%d",&aiMat[iRowCount][iColCount]);
printf("\n");
getchar();

}
}
}


getting core dump in scanning value.

arsh_arsh
Newbie Poster
6 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

i guess u have not initialized iRowCount and iNoOfCol.

Also note that iRowCount<=MAX_ROW and iNoOfCol<=MAX_COL. Otherwise u will get array outofbound error (or seg fault)

dkalita
Posting Pro in Training
402 posts since Sep 2009
Reputation Points: 121
Solved Threads: 61
 

To add to dkalita's reply, the array decaying into a pointer rule only applies to the first dimension. The pointer version of your 2D array function parameter is int (*)[MAX_COL] . You can change your function in either of these two ways:

void fnGetMatrix(int aiMat[][MAX_COL,int *iNoOfRows,int *iNoOfCol)
void fnGetMatrix(int (*aiMat)[MAX_COL],int *iNoOfRows,int *iNoOfCol)

The size in the first dimension does not matter, but it is easier to copy the array declaration and match it exactly so that you do not need to worry about mismatches like the one you discovered:

void fnGetMatrix(int aiMat[MAX_ROW][MAX_COL],int *iNoOfRows,int *iNoOfCol)
Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 

thanks dooing change in function declaration worked .. thanks guys

arsh_arsh
Newbie Poster
6 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You