Why function does not know the array size?

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

If I write

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

        
8条回答
  •  天命终不由人
    2021-01-26 21:33

    A pointer is a pointer. That means, it simply points to memory, and that's all about it. Creating a pointer to an array (which usually means a pointer to the first element of the array, but not necessarily) is still only a pointer to some memory location. As a memory address is simply a memory address there is also no way for the pointer to know that the memory it is pointing to originally was an array, or how long that array was.

    It's simply how pointers work. They point to memory, and that's all.

提交回复
热议问题