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.

Recommended Answers

All 3 Replies

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)

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)

thanks dooing change in function declaration worked .. thanks guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.