+ 2

C++: Questions about array

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
汝風留名
汝風留名 - avatar
49 Respuestas
+ 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
HonFu - avatar
+ 4
汝風留名 An array name contains the base address of the array. Now where does an index starts from? It is 0. So if you have a[0] a[1] a[2] and so on where do you think your array starts from? It starts from a[0] isn't it? So that is why the array name stores the address of the first element. Now consider this example that you are standing in a line and you are the first one in the line. Now let us say all of you have a chest number and you have the number 0 and the person behind you has number 1 and so on. Now tell me if someone calls you by your name wouldn't you respond? Now if someone calls you by your chest number wouldn't you respond? And you are still in the 1st position with 2 references.
1st Dec 2019, 1:43 PM
Avinesh
Avinesh - avatar
+ 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
HonFu - avatar
+ 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
汝風留名
汝風留名 - avatar
+ 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
汝風留名
汝風留名 - avatar
+ 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
汝風留名
汝風留名 - avatar
+ 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
汝風留名
汝風留名 - avatar
+ 3
JoeyCentral, you are being condescending and ignorant right now. It can be perfectly fine to recommend a beginner to wait with a topic, until it can be more easily understood. But that's not what you did. You gave a wrong answer that had no relation to the post, and now you're trying to gloss over it, by saying, a noob doesn't need to know it. Giving a different answer than the one which OP asked makes no sense in any universum and doesn't help. In the worst case it leads to more confusion. Take some time and read the thread - two people, me included, understood the question wrong at first. And that's no problem, it can happen - I also found it a bit confusedly written. But if you had read the thread before you attempted an answer, you would have figured out what this is actually about.
3rd Dec 2019, 5:14 PM
HonFu
HonFu - avatar
+ 2
Thx~
1st Dec 2019, 10:21 AM
汝風留名
汝風留名 - avatar
+ 2
汝風留名 kindly go through this code and tell me what you don't understand. https://code.sololearn.com/csZO5oUQ25QY/?ref=app
1st Dec 2019, 2:09 PM
Avinesh
Avinesh - avatar
+ 2
HonFu Yes,that surprise is what makes me confused xD
1st Dec 2019, 2:17 PM
汝風留名
汝風留名 - avatar
+ 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
Avinesh - avatar
+ 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
HonFu - avatar
+ 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
HonFu - avatar
+ 1
OK👌
1st Dec 2019, 1:21 PM
汝風留名
汝風留名 - avatar
+ 1
ok thx, let me think about it
1st Dec 2019, 1:46 PM
汝風留名
汝風留名 - avatar
+ 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
HonFu - avatar
+ 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
HonFu - avatar
+ 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
HonFu - avatar
+ 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
Avinesh - avatar