differs in levels of in direction from / different types for formal and actual

This is a discussion on differs in levels of in direction from / different types for formal and actual within the C Programming forums, part of the General Programming Boards category; Hi there, the purpose of this program is to find whether numbers along the diagonal of matrix are same or ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    differs in levels of in direction from / different types for formal and actual

    Hi there, the purpose of this program is to find whether numbers along the diagonal of matrix are same or not.


    Code:
    #include <stdio.h>
    int checkdiag(int matrix[][100], int size)
    {
    	int i,j,status;
    	for(i=0; i <size; i++)
    	{
    		for(j=0; j <size; j++)
    		{
    			status = 0;
    			if(matrix[i][j]==matrix[i+1][j+1])
    			{
    				status =1;
    			}
    		}
    	}
    
    	return status;
    }
    
    int main(void)
    {
    	FILE *ifp;
    	int **matrix;
    	int size,i,j,status;
    	ifp = fopen("matrix.txt","r");
    	fscanf(ifp,"%d",&size);
    	matrix = (int**)calloc(size,sizeof(int));
    	for(i=0; i < size; i++)
    	{
    		matrix[i]= (int*)calloc(size,sizeof(int));
    	}
    	while(!feof(ifp))
    	{
    		for(i=0; i< size; i++)
    		{
    			for(j=0; j < size; j++)
    			{
    				fscanf(ifp,"%d",&matrix[i][j]);
    			}
    		}
    	}
    	status = checkdiag(matrix,size);
    	if(status==1)
    	{
    		printf("The Matrix Is %d x %d and and all the numbers on the main diagonal are the same.");
    	}
    	else
    	{
    		printf("Numbers along diagonal are not the same");
    	}
    	fclose(ifp);
    	free(matrix);
    	return 0;
    }
    in file matrix1.txt is this:

    3
    4 5 6
    7 8 9
    3 6 7







    and the error im getting follows:

    lab10.c(42) : warning C4047: 'function' : 'int (*)[100]' differs in levels of in
    direction from 'int **'
    lab10.c(42) : warning C4024: 'checkdiag' : different types for formal and actual
    parameter 1

    Please Help me out: Thank You.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, it seems like a pretty clear message: a two-dimensional array of integers ("int (*)[100]") is not the same thing as a pointer to a pointer to int ("int **"). If you intend to pass such a thing to your function, your function should expect such a thing.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Then, whats the best way to dynamically allocate a 2D array and pass that array into a function?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    I suggest using a little abstraction:
    Code:
    typedef struct
    {
        int **data;
        size_t size;
    } SquareMatrix;
    Now, you write functions that work with SquareMatrix objects (or rather, pointers to SquareMatrix objects). The function can easily access both the data in the matrix, as well as the size (or perhaps more accurately, order) of the matrix.

    Also, instead of having data as a pointer to a pointer, you could make it just a pointer to an int, and then compute offsets.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    sorry but the requirement my prof said was to use

    int checkdiag(int matrix[][100], int size)

    for user defined function... thatswhere im having problem with :|

    If i were to use structure wouldnt it be

    int checkdiag(SquareMatrix variable) ?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    > int checkdiag(int matrix[][100], int size)
    If you've got this, then you need to have either

    int array[100][100];


    or
    int (*array)[100] = malloc( 100 * sizeof *array );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with two two dimensional arrays
    By scmurphy64 in forum C Programming
    Replies: 5
    Last Post: 12-06-2009, 06:57 PM
  2. warning C4013...errrrrr!
    By IndioDoido in forum C Programming
    Replies: 27
    Last Post: 03-21-2008, 03:02 AM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21