0

Here you can find some code to verify by your own.

// a global variable
float array[10];

void countNumber(float array[])
{
    int number= sizeof(array)/sizeof(float);
    printf("NUMBER of array: %d\n", number);
}

int main()
{
    countNumber(array);
    // NOT CORRECT: Result is 2
    int number= sizeof(array)/sizeof(float);
    printf("NUMBER of array: %d\n", number);
    // CORRECT: Result is 10
}

I don´t understand why I get two different result (2 vs. 10 for the size of the array) but the calculations are the same.

Marco
  • 1
  • 1
  • 1
    You can't do that in C! You *MUST* pass the size of the array (e.g. "10") as a *PARAMETER* to your function: `void countNumber(float array[], int size) {...}` – FoggyDay Apr 07 '20 at 21:57
  • 3
    In the function, `array` is a pointer, not an array. – Barmar Apr 07 '20 at 21:57
  • @Barmar: yes. But the real source of the OP's confusion is that he thinks the callee will somehow "know" it's being passed a float array, and the size of that array. In Java or C#: yes. In C: no. The link you cited is a good one :) – FoggyDay Apr 07 '20 at 22:01
  • Of course, that's a common confusion. That's why I told him it's not true. The full details are at the duplicate question. – Barmar Apr 07 '20 at 22:03

0 Answers0