I am new to C++ and to programming in general.
(我是C ++和一般编程人员的新手。)
I got confused when I learnt the concepts of pointer and array.(当我学习了指针和数组的概念时,我感到困惑。)
Takesint*p = arr; int arr[]={5,1};
(取int*p = arr; int arr[]={5,1};
) int*p = arr; int arr[]={5,1};
as an example.
(举个例子。)
I learnt thatarr
is also a pointer. (我了解到arr
也是一个指针。)
p(a pointer) arr[0]
the thing it stores: [first element address: 601] [5]
its memory : 501 601
address(just make
some fake address)
However, arr(the pointer)
[first element address: 601]
601
Normally, a pointer should have a different address from the array.
(通常,指针应具有与数组不同的地址。)
However, thearr
, as the pointer to the first element, has the same address as the first element. (但是,作为指向第一个元素的指针的arr
具有与第一个元素相同的地址。)
(所以我感到困惑。)
And I wonder whether it is because the memory box ofarr[0]
is split into two parts: one part for arr[0]
, and one part for the pointer arr
, so that they have the same address in memory. (而且我想知道是否是因为arr[0]
的存储盒分为两部分: arr[0]
一部分和指针arr
一部分,以便它们在内存中具有相同的地址。)