-2
int main() {

    unsigned __int8 counts[5] = { 0, 0, 0, 0, 0};
    sizeof(counts) //Prints 5
    Func(counts);

}

void Func(unsigned __int8 counts[]){
   sizeof(counts) //Prints 4
}

Could someone explain why sizeof prints two different values for the same array?

cubesnyc
  • 1,385
  • 2
  • 15
  • 32
  • It is not "the same array". In `main` you do indeed have an *array*. In `Func` you don't have an array. `counts` is a *pointer* inside `Func`. – AnT stands with Russia Nov 17 '17 at 08:16
  • 2
    Strictly speaking, interpretation of array parameters as pointer parameters is not "array decaying". "Array decaying" is implicit array-to-pointer conversion. It happens in expressions. It is a value-level phenomenon. Special treatment of array parameters is a type-level phenomenon. It has similarities at superficial level, but it is a different mechanism. It is sometimes included into "array decay", but this inclusion is misleading. – AnT stands with Russia Nov 17 '17 at 08:19

1 Answers1

0

You're passing the array by value and not by reference, this means the pointer is being copied and returning the size of that, rather than the array itself.

Change your function to pass by reference or pointer.

See this answer for more help: What is array decaying?

EM-Creations
  • 4,195
  • 4
  • 40
  • 56