Thread: Passing multidimensional/dynamic array to function

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    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;
    	
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    What error are you getting? In main() matrix is declared as a pointer to int but in checkdiag2() it is being set equal to a 2D array; no can't do.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays degrade to a pointer to their type when passed to functions.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 7
    Last Post: 11-21-2008, 04:27 PM
  3. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM