Thread: 2D array pointers

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    2D array pointers

    I need some help with some 2D pointers and passing arrays to functions. Heres the code i got so far but i get a segmentation fault:
    Code:
    #include <stdio.h>
    
    float avg(int **array, int row, int col)
    {
    	int i,j, count=0;
    	float calculation = 0, average;
    	for(i=0; i<row; i++)
    	{
    		for(j=0; j<col; j++)
    		{
    			calculation += array[i][j];
    			count++;
    		}
    	}
    	average = calculation/count;
    	return average;
    }
    
    int main(void)
    {
    	int i, j;
    	int row=3, col=3, array[row][col];
    	for(i=0; i<row; i++)
    	{
    		for(j=0; j<col; j++)
    		{
    			printf("Enter value %d,%d \n", i, j);
    			scanf("%d",&array[i][j]);
    		}	
    	}
    	float average = avg(&array[0][0], row, col);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    If array is defined as int ** (which, in your function, it is), then "array[i][j]" doesn't do what you want it to do. Namely, it will interpret your array[i] -- the number i spots away from where you started -- as a pointer, not an integer, and will then try to follow that pointer. It's unlikely that you typed in, not only a valid pointer, but the pointers where the right things are stored in memory.

    In summary: int ** and int[][] are not compatible.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    Question

    What would be the fix as I then tried this: (*(*(array+i)+j))
    and still got the error

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    You have to make them match: either the function says
    Code:
    float avg(int array[][3], int row, int col)
    or the variable says
    Code:
    int **array;
    I posted literally in the last 24 hours how to fake a 2D array using int** in the middle of a thread with a long discussion of why and what, so search.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    The function parameters should stay how i have them.
    I tried this:
    ...
    value = &array[i][j];
    calculation += value;
    ....

    Do i need to type cast value? what do i type cast to?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    If the function parameters must stay, then array[row][col] CANNOT work. There is no way to take a 2D array and make it something that an int** can understand. You MUST use int** throughout.

    Edit: Unless you are willing to forgo double-subscripting and calculate all the offsets yourself. (I.e., instead of saying array[2][1] you say something like array[2*row+1].) You'd have to cast your int** down to an int* first, though.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    but i tried this:
    calculation += (*(*(array+i)+j));

    But i still get segmentation fault?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Well, yes. Because (*(*(array+i)+j)) makes no sense. Again, you're using the values you typed in via the for-loop as pointers, and then trying to follow those pointers.... there's nothing there, except the Land of Segfaults and Broken Dreams.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    How do i get that bit working. I think i need to cast something but not sure

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Have you read any of these replies?
    Quote Originally Posted by tabstop View Post
    Edit: Unless you are willing to forgo double-subscripting and calculate all the offsets yourself. (I.e., instead of saying array[2][1] you say something like array[2*row+1].) You'd have to cast your int** down to an int* first, though.
    If for whatever reason you are unable to change the function header and the variable itself (which I still recommend, since this is apparently supposed to be dynamic in some way), then you'll have to put the pointer in an int* variable and calculate the offsets yourself.

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Thats where am confused, how do i do that?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by taurus View Post
    Thats where am confused, how do i do that?
    Do you understand how a 2-D array is laid out in memory?

    Say I define array[3][3]. Then memory contains
    array: [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] [2][0] [2][1] [2][2]
    in that order. Your job is, given [i][j], to find the offset (which item in the list it is).

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    How come i cant do this then
    Calculation += *pointer

    given Calculation is an int
    ?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    You can. But you need to have the pointer first, and brother, (*(array+i)+j) ain't it.

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    As a test:

    calculation += *(array+i);
    Isint that a pointer?

    But i get the error:
    loop.c:25: error: invalid operands to binary +

    and when i do this:
    calculation += (int)(*(array+i));

    Calculation doesnt seem to be getting set.
    Last edited by taurus; 10-20-2008 at 11:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM