hello all quick pointers question

This is a discussion on hello all quick pointers question within the C Programming forums, part of the General Programming Boards category; i had to write this func. on an assignment Code: int checkMat(int**mat , int rows) { int i,j; int sum=0; ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    8

    hello all quick pointers question

    i had to write this func. on an assignment

    Code:
    int checkMat(int**mat , int rows)
    {
    	int i,j;
    	int sum=0;    // will be the sum of the first - the sum of the second if sum=0 then they're equal
    	for (i=0;i<rows;i++)
    		for(j=0;j<rows;j++)
    			sum = sum+mat[i][j]-mat[rows-i][rows-j];
    	if (sum=0) return 1;
    	return 0;
    
    }
    now i want to test it in the main so i did :
    Code:
    	int arr[2][2]={{3,5},
    					{9,4}};
            printf("%d",checkMat(arr[0],6));
    and it gives a "argument of type incompatible" what am i doing wrong in the main ?

    i don't want to change the signature of the function
    tnx all helpers !

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Your function wants int **mat... you are handing it an array.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    8
    so i have to send it a 2d array pointer ? i can't send the array like in regulars arrays ?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    8
    ok i read it i understand why it doesn't work , so i tried to make a pointer to point to the 2d array like so:
    Code:
    	int arr[2][2]={{4,6},{3,6}};
    	int **p ;
    	**p[2]=&arr[0][0];
    i tried other variations in order to make it work , any ideas on how to make a pointer point to the 2d array ?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Code:
    void foo ( int (*arr)[2] ) {
       // do stuff with arr[x][y]
    }
    int main ( ) {
      int arr[2][2]={{4,6},{3,6}};  // 2D array 
      int (*pa)[2] = arr;  // pointer to 2D array (or more accurately, pointer to the first row of the array)
      foo( arr );  // call it
      foo( pa );  // call it again
      return 0;
    }
    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.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    8
    yea ok but my function signature is
    Code:
    int checkMat(int**mat , int rows)
    so i can't do
    Code:
    checkMat(arr)

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    If this is a two-dimensional array, shouldn't you be passing both the row count and the column count to the function? Actually, I guess if it's square it doesn't really matter. I'm just really specific about that sort of stuff.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Well if you have int**, and you can't change it, then you can't pass a true 2D array to it without some additional code.

    Say
    Code:
    int arr[2][2]={{4,6},{3,6}};  // 2D array
    int *pa[2] = { &arr[0][0], &arr[1][0] };
    checkMat( pa, 2 );
    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.

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    8
    yea i forgot to say it was a square
    ok it works now thanks a lot
    can you just explain a little about what you did there ?
    is it like an array of pointers ?

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    int (*pa)[2]
    Pointer to an array.

    int *pa[2]
    Array of pointers.
    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.

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    8
    oh i c , thanks a lot
    it really cleared some issues for me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Quick question on pointers
    By newbie30 in forum C Programming
    Replies: 3
    Last Post: 01-13-2010, 02:46 AM
  3. Quick question arrays/pointers
    By liljp617 in forum C Programming
    Replies: 6
    Last Post: 10-02-2008, 12:11 PM
  4. A quick question about pointers.
    By waynex in forum C Programming
    Replies: 6
    Last Post: 03-04-2008, 06:12 AM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM

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