# Thread: Two dimensional arrays and pointers

1. ## Two dimensional arrays and pointers

I'm not understanding what's happening here. It seems that I'm feeding the function in the wrong way

Code:
```#define LEN 4

int sum_two_dimensional_array(const int a[][LEN], int n)
{
const int *p;
int sum = 0;

for (p = &a; p < &a[n-1][LEN-1]; p++)
sum += *p;

return sum;
}

int main(void)
{
int arr[LEN] = {{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 0, 3, 2}};

printf("The sum is: %d\n", sum_two_dimensional_array(arr, 3));

return 0;
}```
It's just an exercises from a book. modify the function int sum_two_dimensional_array(const int a[][LEN], int n);
Using pointers to eliminate the use of two for loops, variables i & j and the [ ] (which I dont know how can I write the for loop for twodim array without using the [ ] )
Any explanation it's appreciate 2. Maybe this is a better approach:

Code:
```#include <stdio.h>

#define LEN 4

// n = number of "lines", m = number of "columns".
// p pointer to the first element.
int sum_two_dimensional_array ( const int *p,
int n, int m )
{
int sum = 0;

// Points to next address beyond the array boundary.
const int *q = p + m*n;

while ( p < q )
sum += *p++;

return sum;
}

int main ( void )
{
int arr[][LEN] = {
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 0, 3, 2}
};

printf ( "The sum is: %d\n",
sum_two_dimensional_array ( (const int *)arr,
3, 4 ) );

return 0;
}``` Popular pages Recent additions a[][len], int, pointers, return, sum 