I am trying to make function which will add up all the elements in an array, but I keep failing. Is something wrong with pointers, or something else? I would appreciate any help.
#include <stdio.h>
int element_sum(int *data)
{
int x = 0; //sum of all elements
for (int i = 0; i < 3; i++) //loop for row
{
for (int j = 0; j < 4; j++) //loop for column
{
x += &data[i][j];
printf("%d\n", x);
}
}
return x;
}
int main(void)
{
int data[3][4] = {{22, 23, 123, 192}, {43, 335, 44, 9}, {3, 93, 8, 7}};
int sum; // sum
sum = element_sum(*data); //function
printf("sum = %d\n", sum);
return 0;
}