1

I am trying to get the values of diagonal values from a 2D array. For example..

10 20 30
10 20 30
10 20 30 

From my codes, I will be adding/summing the numbers from index[0][0] with index1 and index[2][2] USING POINTERS which will compute to 60. However, when I build and run, it returns computation of the memory address instead. Anyone can explain the problem here? (I'm new to C programming and pointers)

void diagonals2D(int array[][SIZE], int rowSize, int colSize, int *sum)
{
    int count;
    *sum=0;

    for(count=0;count<SIZE;count++)
    {
        (*sum)+=*(array+count+count);
    }

}

enter image description here

5
  • 2
    *(array+count+count) --> array[count][count] Commented Sep 10, 2017 at 9:59
  • 3
    @Han (*sum)+=*( *( array+count )+count); Commented Sep 10, 2017 at 10:08
  • I've edited my answer and it seems to work, check it out Commented Sep 10, 2017 at 10:33
  • How is the 'USING POINTERS' constraint useful to future users/visitors? The trivial [xy][xy] indexing is much clearer. Commented Sep 10, 2017 at 10:39
  • oh thank you! it's working :D Commented Sep 10, 2017 at 10:44

3 Answers 3

3

The first parameter is adjusted to

int ( *array )[SIZE]

that is it is a pointer.

So the expression array + count also has the type int ( * )[SIZE]. To get the corresponding "row" of the array you have to dereference the pointer

*( array + count )

In this case you will have an object of the type int[SIZE] that in turn in the expression

*( array + count ) + count

is implicitly convered to the type int *.

Now dereferencing the whole expression you will get the target element

*( *( array + count ) + count )

It is better to declare the function as having the parameter of the variable length array.

Here is a demonstration program

#include <stdio.h>

void diagonals2D( long long int *sum, size_t n, int a[n][n] )
{
    *sum = 0;

    for ( size_t i = 0; i < n; i++ )
    {
        *sum += *( *( a + i ) + i );
    }
}

#define N   3

int main(void) 
{
    int a[][N] =
    {
        { 10, 20, 30 },
        { 10, 20, 30 },
        { 10, 20, 30 }      
    };

    long long int sum;

    diagonals2D( &sum, N, a );

    printf( "sum = %lld\n", sum );

    return 0;
}

Its output is

sum = 60
Sign up to request clarification or add additional context in comments.

1 Comment

@Han See my demonstrative program and compare its output with your output to find the difference. Maybe in a printf call you are using expression &sum instead of sum.
0

That array+count+count is not equivalent to array[count][count], that is what you need to achieve.
The best way you could achieve it is the cleaner array[count][count] but if you want to stick with pointers arithmetic from a learning point of view (again, this is bad for readability and debugging) you should access the element this way:

(*sum)+=*(*(array+count)+count);

This way you add count to the base pointer array, memory location of the base address of the countth row. It's a pointer to a pointer.

Dereferencing it will give you the base address of the row.

Adding count to the base pointer for the row will give you the address of the countth element of the row, and dereferencing it will give you the value of the element.

See the code here

6 Comments

In the OP's snippet's context "*(array + count * SIZE + count)" does not evaluate to an int!
what do you mean? The memory location that's dereferenced looks ok to me. Let me try, wait
I want to say that int array[][SIZE] and int array[SIZE] are not the same.
Doing int i = *(array + count * SIZE + count); would not do what you expect.
I'm sorry if I wrote wrong stuff before, hope I didn't confuse you!
|
0

As the given problem,you should use the following code.

for(count = 0; count < size; count++)
   sum=*( *( array + count ) + count);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.