Why function does not know the array size?

后端 未结 8 2162
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-26 21:04

If I write

int main()
{
    int a[100] = {1,2,3,4,};
    cout<

        
8条回答
  •  星月不相逢
    2021-01-26 21:54

    sizeof returns the size of the type. In the second example, func( int *a ), a is a pointer and sizeof will report it as such. Even if you did func( int a[100] ), a would be a pointer. If you want the size of the array in func, you must pass it as an extra argument.

提交回复
热议问题