I have heard that when using an array as a function argument, the compiler takes it as not actually an array but as a pointer to the 1st element of the array. So, when asking for size of A(int array) it should return size of pointer 4 (in bytes). But I am getting 8. What is the problem?
I am using CodeBlocks.
int SumOfElemets(int* A, int size){// int* A or int A[] ... it is the same!
int i, sum = 0;
printf("SOE - Size of A = %d, size of A[0] = %d\n", sizeof(A), sizeof(A[0]));
//here it should be 4 and 4 but i am getting 8 and 4
for (i=0;i<size; i++){
sum += A[i]; //A[i] is *(A+i)
}
return sum;
}
int main(){
int A[] = {1,2,3,4,5};
int size = sizeof(A)/sizeof(A[0]); //size of the array
int total = SumOfElemets(A, size);
printf("Sum of elements = %d\n", total);
printf("Main - Size of A = %d, size of A[0] = %d\n", sizeof(A), sizeof(A[0]));
return 0;
}