Passing multidimensional/dynamic array to function
hi. im having trying with this program im working on. Basically im trying to pass a 2D array to a function. Then tests for a antidiagonal. But im getting errors in passing the array. Would someone point out the error to me?
Thanks
Code:
#include <stdio.h>
#define row 100
#define col 100
int checkdiag2( int matrix[row][col], int size );
main()
{
int *matrix;
int size, result, i, j;
FILE *ifp;
ifp = fopen("matrix3.txt", "r");
fscanf(ifp, "%d", &size);
matrix = (int *) malloc ( sizeof(int) * size );
for ( i=0; i < size; i++)
for ( j=0; j < size; j++ )
fscanf(ifp, "%d", &matrix[i][j]);
printf("Matrix:\n");
for ( i=0; i < size; i++)
{
for ( j=0; j < size; j++ )
printf("%d", &matrix[i][j]);
printf("\n");
}
if ( checkdiag2(matrix, size) == 1 )
printf("\nThe matrix is %d x %d and all the numbers on the anti diagonal are the same.\n", size, size);
else
printf("The matrix is %d x %d and all the numbers on the anti diagonal are NOT the same.", size, size);
fclose(ifp);
}
int checkdiag2( int matrix[row][col], int size )
{
int i, j, temp;
temp = matrix[0][size];
for ( i=0; i < size; i++ )
for ( j=0; j < size; j++ )
if ( i == size - j )
if ( matrix[i][j] != temp )
return 0;
return 1;
}