C++: Questions about array | Sololearn: Learn to code for FREE!

+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?

12/1/2019 10:02:26 AM

汝風留名

49 Answers

New Answer

+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.

+6

Avinesh I think after C++ templates, pointers are the most dreaded and notorious programming concept 😁😇😉

+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.

+4

HonFu Array is indeed not a pointer, rather it decays into a pointer. A N dimension array decays to N-1 dimension when assigned to a pointer or passed to a function with the only exception when passed to the sizeof operator where it does not decay because sizeof works at compile time.

+4

JoeyCentral, so since OP is a 'noob' we just answer a different question than the one they actually asked?

+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?

+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.

+3

HonFu Address of array or array name decays to address of the first element the difference comes when looking from the other side (pointer side) say you have an arbitary address 0x7ff3171788 if you convert it to char*, or int*, or float* etc then the initial address is going to be same. but then the next address will be according to the type of the pointer

+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.

+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.

+3

汝風留名 Array and pointers are not same. Name of the array points to the whole array but when an array is assigned to a pointer or passed to a function it decays to the address of of the first element, it is designed that way. You are confusing yourself. A[i] = *(A + i) the value is stored in the address (A+i) where A is the base address (address of the first element), and to access that value you dereference the pointer (see that '*'). You can also use array name as pointer because the memory allocated to the array is contiguous.

+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.

+2

+2

汝風留名 kindly go through this code and tell me what you don't understand. https://code.sololearn.com/csZO5oUQ25QY/?ref=app

+2

HonFu Yes,that surprise is what makes me confused xD

+2

given int arr[4] {1,2,3,4}; int *p = &arr is illegal. Compiler should issue a warning if not error "incompatible pointer types" . Doing this can result in undefined behavior. The type of &arr is pointer to an array of 4 int so your pointer needs to be of that type int (*p)[4] = &arr; If you now do p++ the address will increase by 4 * sizeof(int) not by sizeof(int) When you assign an array to a pointer then it decays into the address of the first element int* p = arr; // decays into the address of first element It is same as int *p = &arr[0]; But irrespective of that all pointers will give the same initial address.

+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.

+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.

+1

Don't be sorry. Let me think about it for a little while, I'll try again. 😅

+1