1

I am confused about array usage as pointer and result of that. Let me explain. When I try this

#include <iostream>

using namespace std;

int main()
{
    int a = 10;
    int *myPointer = &a;
    cout << "*myPointer: \t" << *myPointer << endl;
    cout << "myPointer: \t" << myPointer << endl;
    cout << "&myPointer: \t" << &myPointer << endl;
    cout << "myPointer[0]: \t" << myPointer[0] << endl;

    cout << endl;

    int myArray[3] = {1,2,3};
    cout << "*myArray: \t" << *myArray << endl;
    cout << "myArray: \t" << myArray << endl;
    cout << "&myArray: \t" << &myArray << endl;

    return 0;
}

All of output are exactly what I expected, except last one (&myArray). Lets say my ram something like this:

Address Variable Value
0xA100 a 10
0xA101 myPtr 0xA100
0xA102 1
0xA103 2
0xA104 3
0xA105 myArray 0xA102

If I imagine it correctly, then how "&myArray" can be same with "myArray"? Actually, I think it can be anything but 0xA102. Because, this result means that "myArray" is in 0xA102 and value of pointed by myArray is in 0xA102 too. And I know it is weird but it means also, as a value "1", is in 0xA102 too. (At least I got it from this result).

So, I cannot catch the point. I think the result has to be 0xA105 if "&" means address. If yes, why result is not 0xA105. If not, why they have different usage although arrays are pointers. Is there anybody can clarify the matter? Thanks.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
detraqueur
  • 11
  • 1
  • 1
    Your model is wrong for myArray. In your version myArray is just like a pointer with it's own separate location in memory, but that is not correct. myArray is at the address 0xA102, not 0xA105. – john Feb 21 '21 at 20:24
  • Please don't add both c and c++ tags to a question. They're different languages and the answers will usually be different for both of them. – cigien Feb 21 '21 at 20:26
  • Arrays are not pointers. Even though an array can be implicitly converted to the pointer to its first element, that pointer isn't stored anywhere and is computed on the fly, so John is correct. `&myArray` isn't `int**` ("pointer to pointer to int", like `&myPointer`); it's `int(*)[3]` ("pointer to array of size 3 of int"). – HolyBlackCat Feb 21 '21 at 20:26
  • @john I thought that but if it is in 102 is not myArray behave like an integer ? and result will be something like that: myArray = 1 ? – detraqueur Feb 21 '21 at 21:56
  • 1
    @cigien I know they are different but I want to get the logic of this topic/issue which I know C also can explain what is happening behind the scenes, because this is not C++ specific topic but common/mutual. That is why I have added C. I tried to remove it but it has already been removed. Thank you for keep me informed. – detraqueur Feb 21 '21 at 22:04
  • `myArray` is not **in** location 0xA102 it is a reference to the address 0xA102. – john Feb 21 '21 at 22:15
  • @HolyBlackCat _that pointer isn't stored anywhere and is computed on the fly_ is the sentence which I cannot understand. If somehow we can use anything without store it why we dont use pointers as same way without waste some storage? If it was stored on ram everything would be crystal clear for me. – detraqueur Feb 21 '21 at 22:16
  • @detraqueur Because the **value** of a pointer can be modifed, so it has to exist in a memory location. But an array doesn't have a value which can be altered, so it doesn't have to be stored in a particular location. – john Feb 21 '21 at 22:18
  • @detraqueur Arrays are special, they are not variables like integers or pointers. – john Feb 21 '21 at 22:20
  • @john I thought you have meant it with this sentence _myArray is at the address 0xA102, not 0xA105_. Still I cannot understand how we can use it without store as I asked HolyBlackCat – detraqueur Feb 21 '21 at 22:22
  • The compiler knows where it is, It generates the correct code to access the array elements. But you cannot change where those elements are. That's why the location isn't explicitly stored anywhere in your program. – john Feb 21 '21 at 22:25
  • @john finally I think I got it. That is answer which I could not find anywhere. Thank you a lot. It is clear for me now how it is possible for arrays and not for pointers. I knew that arrays addresses is constant, but I've never look from this aspect. Thank you again. – detraqueur Feb 21 '21 at 22:27
  • @detraqueur Glad to help, not sure I explained things very well. – john Feb 21 '21 at 22:29
  • @john Yes you did. I could not mark your answer as a solution, stackoverflow does not give this option for newbies as I understand. Anyway, thank you again. – detraqueur Feb 21 '21 at 22:49

0 Answers0