Why function does not know the array size?

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

If I write

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

        
8条回答
  •  南方客
    南方客 (楼主)
    2021-01-26 21:39

    This isn't working because sizeof is calculated at compile-time. The function has no information about the size of its parameter (it only knows that it points to a memory address).

    Consider using an STL vector instead, or passing in array sizes as parameters to functions.

    This was answered by Marcel Guzman in Calculating size of an array!

提交回复
热议问题