Im running through a tutorial on pointers to refresh my brain a little and came to a section about passing arrays as parameters to functions. Ive attached code below
int SumOfElements(int A[], int len){
int sum;
for(int i = 0; i < len; i++){
sum++;
}
return sum;
}
This function works fine and when i pass an array like A[5] = {1,2,3,4,5} i get the expected output of 5 when passed to main(). However, if i do not declare the length of the array and pass it into the function, writing instead:
int SumOfElements(int A[]){
int sum;
int len = sizeof(A)/sizeof(A[0]);
for(int i = 0; i < len; i++){
sum++;
}
return sum;
}
I find that the result, sum is 2 in the main function. Why is this happening?