Новый курс! Каждый программист должен знать генеративный ИИ!
+ 2

I am a beginner for programming. I got confused about the concepts of array. For example, int arr[5]={blabla....}; If I cout arr, it outputs the base address of the array, which is the address of the first element. So I thought that arr stored the base address of the array as its value. Just like int a = 5, which "a" stored value of 5. And thus arr should have its own address. However, when I cout &arr, it gave me the same result as its value. So does it mean that the memory box for arr[0] has split into 2 parts: one part stores its own address so that when I cout arr, it gives me the address; another part stores the value of the first element so that when I cout arr[0], it gives me the value?

1st Dec 2019, 10:02 AM

42 ответов
+ 7
arr is the pointer to the first element, so it shows that address. Like with regular pointers you can write *arr, then you get the first element. To get a later element, you would have to add the size of one element (this case an int) to the adress. So you'd have to step forward the length of an int in memory. Since the pointer knows its type size, you can write *(a+1) to do that and get the next element. And basically a[1] is just another way of writing that.
1st Dec 2019, 10:18 AM
HonFu
+ 4

1st Dec 2019, 1:43 PM
Avinesh
+ 4
JoeyCentral, so since OP is a 'noob' we just answer a different question than the one they actually asked?
3rd Dec 2019, 4:51 PM
HonFu
+ 3
Sorry XD I still don't get it. #include <iostream> #include <ios> int main() { int a = 5; int* p = &a; std::cout << &p << " " << &a << std::endl; } From the normal case above, the output shows that normal pointer has a different address from the variable "a". It reflects that a pointer and the variable it points to should occupy different addresses in the memory. However, arr, as the pointer to the first element, takes the same memory address as the first element it points to. Why?
1st Dec 2019, 11:50 AM

+ 3
Em.....I am sorry but I still don't think that my question is solved. Takes int*p = arr; int arr={5,1}; as an example. 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, the arr, as the pointer to the first element, has the same address as the first element. So I feel confused. And thus my very first question mentioned that I wonder whether it is because the memory box of arr[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.
1st Dec 2019, 1:09 PM

+ 3
HonFu xD I am just a beginner, and a self-learner. So I probably wont know more than you. But I will try my best to figure out what ~swim~ has mentioned.
1st Dec 2019, 4:26 PM

+ 3
@HonFu @Avinesh I still cannot figure out what it means. So I asked it on StackOverflow. The reply is as follow: I would suggest that pointers are not a beginner topic in C++, they are mostly just a carry over from C. If you can, you should avoid them and use the STL Containers. In your code sample, the type of arr is int[2]. You should think of that in memory as looking something like this: arr --+ | v +---+---+ | 5 | 1 | +---+---+ The value contained in arr is the location of the first element (the 5). arr is essentially a pointer to that 5. The only difference being that the type (int[2]) has also remembered how many elements there are. The assignment statement p = arr works because p's type is int* which int[] can decay to.
1st Dec 2019, 5:27 PM

+ 3
3rd Dec 2019, 5:14 PM
HonFu
+ 2
Thx~
1st Dec 2019, 10:21 AM

+ 2

1st Dec 2019, 2:09 PM
Avinesh
+ 2
HonFu Yes,that surprise is what makes me confused xD
1st Dec 2019, 2:17 PM

+ 2
~ swim ~ HonFu I'm a beginner myself and I have not jumped deep into C++. I have just learnt enough to feel comfortable with it. I'm learning java now so only through QA I'm in touch with C and C++. You guys have been a great support indirectly in helping me learn some more concepts in programming. So keep rocking. People like me are benefitted for sure.
1st Dec 2019, 4:34 PM
Avinesh
+ 1
arr, if you're not indexing, gives the address - because arr is a pointer. int *p = arr; // same as &a in your example p is another pointer to the first element. So now you have two pointers pointing to exactly the same spot in memory - so the same address has to be stored in them.
1st Dec 2019, 12:05 PM
HonFu
+ 1
Don't be sorry. Let me think about it for a little while, I'll try again. 😅
1st Dec 2019, 1:20 PM
HonFu
+ 1
OK👌
1st Dec 2019, 1:21 PM

+ 1
ok thx, let me think about it
1st Dec 2019, 1:46 PM

+ 1
Hm, maybe now I'm beginning to see the issue. The addresses stored in the pointers are the same, which makes sense. &arr[0] equals arr equals p. Now &p is different to p, because one is the address of the pointer, the other the address of the value pointed to by the pointer. This also makes sense. Now if you expect that arr - seen as a pointer - would behave the same, you're in for a surprise! &arr EQUALS arr, so the address IN the pointer equals the address OF the pointer. oO I'm now officially confused myself and have to call for reinforcement - ~ swim ~. 😅
1st Dec 2019, 1:57 PM
HonFu
+ 1
~ swim ~, so far so good - but why do these two lines... cout << arr << endl; cout << &arr << endl; ... give the same address? Shouldn't the address of that array be different to the address of A POINTER to that array?
1st Dec 2019, 2:49 PM
HonFu
+ 1
Sorry, I'm being slow about this one. I wonder what 汝風留名 thinks. Is the issue that arr is not really a pointer, although you can do about anything with it like it was one?
1st Dec 2019, 3:36 PM
HonFu
+ 1
I think that ~ swim ~ has explained it quite well. For some reason we people are confusing ourselves to understand this little concept of pointers.
1st Dec 2019, 3:40 PM
Avinesh
Актуальное сегодня
coding error