pointers - C Programming - Sum of 2D arrays -


i trying values of diagonal values 2d array. example..

10 20 30 10 20 30 10 20 30  

from codes, adding/summing numbers index[0][0] index1 , index[2][2] using pointers compute 60. however, when build , run, returns computation of memory address instead. can explain problem here? (i'm new c programming , 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

the first parameter adjusted to

int ( *array )[size] 

that is pointer.

so expression array + count has type int ( * )[size]. corresponding "row" of array have dereference pointer

*( array + count ) 

in case have object of type int[size] in turn in expression

*( array + count ) + count 

is implicitly convered type int *.

now dereferencing whole expression target element

*( *( array + count ) + count ) 

it better declare function having parameter of variable length array.

here demonstration program

#include <stdio.h>  void diagonals2d( long long int *sum, size_t n, int a[n][n] ) {     *sum = 0;      ( size_t = 0; < n; i++ )     {         *sum += *( *( + ) + );     } }  #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, );      printf( "sum = %lld\n", sum );      return 0; } 

its output is

sum = 60 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -