0

I'm learning about C Pointer from freeCodeCamp.org and I'm getting stuck at the Array pointer as the function arguments follow by this.

In the instructor code was like this

int SumOfElement(int A[]) {
    int i, sum = 0;
    int size = sizeof(A)/sizeof(A[0]);
    printf("SOE - Size of A = %d, size of A[0] = %d", sizeof(A), sizeof(A[0]));
    for(i = 0; i<size; i++)
    { sum += A[i];}
    return sum;
}

int main() {
    int A[]= {1,2,3,4,5};
    int total = SumOfElements(A);
    printf("Sum of elements %d\n",total);
    printf("Main - Size of A = %d, size of A[0] %d",sizeof(A),sizeof(A[0]));
}

And his result (from interested part) is :

SOE - Size of A = 4
Main - Size of A = 20

I understood why's that. But in my code when I try to run similar code I got 8 from printing sizeof(A) inside function

int myFunc(int a[]) {
    printf("Size of a[] inside function is %d\n", sizeof(a));
    return a;
}

int main(){
    int a[] = {1, 2, 3, 4, 5};
    myFunc(a); // Result is 8
    printf("Size of a[] in main function is %d\n", sizeof(a)); // Result is 20
    return 0;
}
user438383
  • 5,716
  • 8
  • 28
  • 43
Jedsada
  • 7
  • 3
  • 3
    It means teacher's pointers are 32-bit and your pointers are 64-bit – pmg Feb 28 '22 at 14:07
  • In the main a is 5 int. in function a is just a pointer to an array so it’s size is just size of a pointer. Total size of array is not passed inside functions. – Ptit Xav Feb 28 '22 at 14:22
  • They should have used `"%zu"` as format specifier, BTW. I hope you got their point, though, which is the main lesson, here. – Bob__ Feb 28 '22 at 14:25
  • 1
    Is that **really** your instructor's code? And is the point to process all the elements of the array? Because given a function `int SumOfElement(int A[])`, `int size = sizeof(A)/sizeof(A[0]);` is ***wrong***. `sizeof(A)` is the size of an `int *` **pointer**, and has nothing to do with the size of the actual array. See [**C sizeof a passed array**](https://stackoverflow.com/questions/5493281/c-sizeof-a-passed-array) If your instructor isn't showing you this code to teach you how wrong it is, find another instructor. – Andrew Henle Feb 28 '22 at 14:58
  • Does this answer your question? [How to find the 'sizeof' (a pointer pointing to an array)?](https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array) – Mgetz Feb 28 '22 at 15:30
  • 1
    @AndrewHenle Yes, He told me it's wrong. He just showed the example. – Jedsada Feb 28 '22 at 16:12

1 Answers1

2

int A[] is an array declaration, but since it is part of a function parameter list, it gets implicitly adjusted into a pointer to the first element of that array.

int SumOfElement(int A[]) is 100% equivalent to int SumOfElement(int* A).

Therefore it is senseless to do sizeof A inside that function, because doing so will always give you the size of the pointer. Which is typically 4 bytes on a 32 bit system but 8 bytes on a 64 bit system.

For the same reason, int size = sizeof(A)/sizeof(A[0]); is nonsense. You can't calculate an array size like that when A is an array which has decayed into a pointer. This gives you the size of a pointer divided with the size of an int.

For this function to make sense, it should probably have a separate size parameter.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • From the question: "I understood why's that". The OP are asking why they get *8* instead of *4*, not why they don't get 20 as in `main`. They just assumed that all the pointers have size 4. – Bob__ Feb 28 '22 at 15:39
  • @Bob__ Yes and I answered that too, so...? – Lundin Feb 28 '22 at 15:53