Find the sum in 2d array with pointers in c -
i have tried "sum=6" , wrong. whats wrong code? here code:
#include <stdio.h> #define row 2 #define col 3 int sum(int(*array)[3]); int main(void) { int a[row][col] = { {1 , 2, 3} , {4 , 5, 6} }; printf(" sum = %d\n", sum (a)); return 0; } int sum(int(*array)[3]) { int i,j, sum = 0; (i =0; < row ; ++) { (j =0; j < col ; j ++) { sum = sum + *(*( array +i )+j); } } }
you forget return value of sum
in function
int sum(int(*array)[3]) { int i,j, sum = 0; (i =0; < row ; ++) { (j =0; j < col ; j ++) { sum = sum + *(*( array +i )+j); } } return sum; /* here */ }
and notice
sum = sum + array[i][j];
is more readable
sum = sum + *(*( array +i )+j);
Comments
Post a Comment