Why are `&array` and `array` pointing to the same address?difference between pointer to an array and...
How to read the error when writing vector files in QGIS 3.0
How can I make my enemies feel real and make combat more engaging?
Can "ee" appear in Latin?
Identical projects by students at two different colleges: still plagiarism?
Sauna: Wood does not feel so hot
我可不觉得 - agree or disagree?
What did Putin say about a US deep state in his state-of-the-nation speech; what has he said in the past?
How to achieve physical gender equality?
Why do we divide Permutations to get to Combinations?
Can I legally make a website about boycotting a certain company?
Last Reboot commands don't agree
Can a planet be tidally unlocked?
What is formjacking?
In a world with multiracial creatures, what word can be used instead of mankind?
How can I portray body horror and still be sensitive to people with disabilities?
Arizona laws regarding ownership of ground glassware for chemistry usage
How to scroll to next div using Javascript?
Will linear voltage regulator step up current?
How do I add a strong "onion flavor" to the biryani (in restaurant style)?
Is Screenshot Time-tracking Common?
Face Value of SOFR futures
Are encryption algorithms with fixed-point free permutations inherently flawed?
Reading source code and extracting json from a url
Why is the meaning of kanji 閑 "leisure"?
Why are `&array` and `array` pointing to the same address?
difference between pointer to an array and pointer to the first element of an arrayCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?With arrays, why is it the case that a[5] == 5[a]?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Why should C++ programmers minimize use of 'new'?For-each over an array in JavaScript?Why is it faster to process a sorted array than an unsorted array?
Until know, I thought an array is the same as a pointer. But I found a weird case:
code
int array[5] = { 10,11,12,13,14};
std::cout << array << std::endl;
std::cout << &array << std::endl;
std::cout << &array[0] << std::endl;
int *pArray = new int[5];
std::cout << pArray << std::endl;
std::cout << &pArray << std::endl;
std::cout << &pArray[0] << std::endl;
output
0x7ffeed730ad0
0x7ffeed730ad0
0x7ffeed730ad0
0x7f906d400340
0x7ffeed730a30
0x7f906d400340
As you can see array
and &array
have the same value. But pArray
and &pArray
have different value. If array is same as pointer, address of array should be different from array.
How can array
and &array
be the same? If array
and &array
are same, what is the address of the memory which holds the array values?
c++ arrays pointers
add a comment |
Until know, I thought an array is the same as a pointer. But I found a weird case:
code
int array[5] = { 10,11,12,13,14};
std::cout << array << std::endl;
std::cout << &array << std::endl;
std::cout << &array[0] << std::endl;
int *pArray = new int[5];
std::cout << pArray << std::endl;
std::cout << &pArray << std::endl;
std::cout << &pArray[0] << std::endl;
output
0x7ffeed730ad0
0x7ffeed730ad0
0x7ffeed730ad0
0x7f906d400340
0x7ffeed730a30
0x7f906d400340
As you can see array
and &array
have the same value. But pArray
and &pArray
have different value. If array is same as pointer, address of array should be different from array.
How can array
and &array
be the same? If array
and &array
are same, what is the address of the memory which holds the array values?
c++ arrays pointers
2
It's because an array is not a pointer. In the case of an array&a
anda
are the same thing. In the case of a pointerp
is the address the pointer points to, and&p
is the address where the pointer value is stored.
– Jabberwocky
1 hour ago
Here we can witness the inconsistency of the language.new int[5]
should return a pointer toint[5]
, not a pointer toint
.
– geza
1 hour ago
related/duplicate: stackoverflow.com/questions/24104482/…
– geza
1 hour ago
add a comment |
Until know, I thought an array is the same as a pointer. But I found a weird case:
code
int array[5] = { 10,11,12,13,14};
std::cout << array << std::endl;
std::cout << &array << std::endl;
std::cout << &array[0] << std::endl;
int *pArray = new int[5];
std::cout << pArray << std::endl;
std::cout << &pArray << std::endl;
std::cout << &pArray[0] << std::endl;
output
0x7ffeed730ad0
0x7ffeed730ad0
0x7ffeed730ad0
0x7f906d400340
0x7ffeed730a30
0x7f906d400340
As you can see array
and &array
have the same value. But pArray
and &pArray
have different value. If array is same as pointer, address of array should be different from array.
How can array
and &array
be the same? If array
and &array
are same, what is the address of the memory which holds the array values?
c++ arrays pointers
Until know, I thought an array is the same as a pointer. But I found a weird case:
code
int array[5] = { 10,11,12,13,14};
std::cout << array << std::endl;
std::cout << &array << std::endl;
std::cout << &array[0] << std::endl;
int *pArray = new int[5];
std::cout << pArray << std::endl;
std::cout << &pArray << std::endl;
std::cout << &pArray[0] << std::endl;
output
0x7ffeed730ad0
0x7ffeed730ad0
0x7ffeed730ad0
0x7f906d400340
0x7ffeed730a30
0x7f906d400340
As you can see array
and &array
have the same value. But pArray
and &pArray
have different value. If array is same as pointer, address of array should be different from array.
How can array
and &array
be the same? If array
and &array
are same, what is the address of the memory which holds the array values?
c++ arrays pointers
c++ arrays pointers
edited 1 hour ago
Micha Wiedenmann
10.5k1364105
10.5k1364105
asked 1 hour ago
jinhwanjinhwan
3981319
3981319
2
It's because an array is not a pointer. In the case of an array&a
anda
are the same thing. In the case of a pointerp
is the address the pointer points to, and&p
is the address where the pointer value is stored.
– Jabberwocky
1 hour ago
Here we can witness the inconsistency of the language.new int[5]
should return a pointer toint[5]
, not a pointer toint
.
– geza
1 hour ago
related/duplicate: stackoverflow.com/questions/24104482/…
– geza
1 hour ago
add a comment |
2
It's because an array is not a pointer. In the case of an array&a
anda
are the same thing. In the case of a pointerp
is the address the pointer points to, and&p
is the address where the pointer value is stored.
– Jabberwocky
1 hour ago
Here we can witness the inconsistency of the language.new int[5]
should return a pointer toint[5]
, not a pointer toint
.
– geza
1 hour ago
related/duplicate: stackoverflow.com/questions/24104482/…
– geza
1 hour ago
2
2
It's because an array is not a pointer. In the case of an array
&a
and a
are the same thing. In the case of a pointer p
is the address the pointer points to, and &p
is the address where the pointer value is stored.– Jabberwocky
1 hour ago
It's because an array is not a pointer. In the case of an array
&a
and a
are the same thing. In the case of a pointer p
is the address the pointer points to, and &p
is the address where the pointer value is stored.– Jabberwocky
1 hour ago
Here we can witness the inconsistency of the language.
new int[5]
should return a pointer to int[5]
, not a pointer to int
.– geza
1 hour ago
Here we can witness the inconsistency of the language.
new int[5]
should return a pointer to int[5]
, not a pointer to int
.– geza
1 hour ago
related/duplicate: stackoverflow.com/questions/24104482/…
– geza
1 hour ago
related/duplicate: stackoverflow.com/questions/24104482/…
– geza
1 hour ago
add a comment |
1 Answer
1
active
oldest
votes
Plain array
decays to a pointer to its first element, it's equal to &array[0]
. The first element also happens to start at the same address as the array itself. Hence &array == &array[0]
.
But it's important to note that the types are different:
- The type of
&array[0]
is (in your example)int*
. - The type of
&array
isint(*)[5]
.
The relationship between &array[0]
and &array
might be easier if I show it a little more "graphically" (with pointers added):
+----------+----------+----------+----------+----------+
| array[0] | array[1] | array[2] | array[3] | array[4] |
+----------+----------+----------+----------+----------+
^
|
&array[0]
|
&array
Things are different with pointers though. The pointer pArray
is pointing to some memory, the value of pArray
is the location of that memory. This is what you get when you use pArray
. It is also the same as &pArray[0]
.
When you use &pArray
you get a pointer to the pointer. That is, you get the location (address) of the variable pArray
itself. Its type is int**
.
Somewhat graphical with the pointer pArray
it would be something like this
+--------+ +-----------+-----------+-----------+-----------+-----------+-----+
| pArray | ----> | pArray[0] | pArray[1] | pArray[2] | pArray[3] | pArray[4] | ... |
+--------+ +-----------+-----------+-----------+-----------+-----------+-----+
^ ^
| |
&pArray &pArray[0]
[Note the ...
at the end of the "array", that's because pointers retains no information about the memory it points to. A pointer is only pointing to a specific location, the "first" element of the "array". Treating the memory as an "array" is up to the programmer.]
6
Excellent ASCII art. Much better than the two hundred words I was about to post.
– molbdnilo
1 hour ago
Not sure that(void*)&array
should be equal to(void*)(&array[0])
BTW. (&array[0]
andarray
should).
– Jarod42
10 mins ago
@Jarod42 An array overlaps exactly with all individual elements of the array. There's no "padding" or "gaps" before, between or after the elements of the array.
– Some programmer dude
2 mins ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54807208%2fwhy-are-array-and-array-pointing-to-the-same-address%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Plain array
decays to a pointer to its first element, it's equal to &array[0]
. The first element also happens to start at the same address as the array itself. Hence &array == &array[0]
.
But it's important to note that the types are different:
- The type of
&array[0]
is (in your example)int*
. - The type of
&array
isint(*)[5]
.
The relationship between &array[0]
and &array
might be easier if I show it a little more "graphically" (with pointers added):
+----------+----------+----------+----------+----------+
| array[0] | array[1] | array[2] | array[3] | array[4] |
+----------+----------+----------+----------+----------+
^
|
&array[0]
|
&array
Things are different with pointers though. The pointer pArray
is pointing to some memory, the value of pArray
is the location of that memory. This is what you get when you use pArray
. It is also the same as &pArray[0]
.
When you use &pArray
you get a pointer to the pointer. That is, you get the location (address) of the variable pArray
itself. Its type is int**
.
Somewhat graphical with the pointer pArray
it would be something like this
+--------+ +-----------+-----------+-----------+-----------+-----------+-----+
| pArray | ----> | pArray[0] | pArray[1] | pArray[2] | pArray[3] | pArray[4] | ... |
+--------+ +-----------+-----------+-----------+-----------+-----------+-----+
^ ^
| |
&pArray &pArray[0]
[Note the ...
at the end of the "array", that's because pointers retains no information about the memory it points to. A pointer is only pointing to a specific location, the "first" element of the "array". Treating the memory as an "array" is up to the programmer.]
6
Excellent ASCII art. Much better than the two hundred words I was about to post.
– molbdnilo
1 hour ago
Not sure that(void*)&array
should be equal to(void*)(&array[0])
BTW. (&array[0]
andarray
should).
– Jarod42
10 mins ago
@Jarod42 An array overlaps exactly with all individual elements of the array. There's no "padding" or "gaps" before, between or after the elements of the array.
– Some programmer dude
2 mins ago
add a comment |
Plain array
decays to a pointer to its first element, it's equal to &array[0]
. The first element also happens to start at the same address as the array itself. Hence &array == &array[0]
.
But it's important to note that the types are different:
- The type of
&array[0]
is (in your example)int*
. - The type of
&array
isint(*)[5]
.
The relationship between &array[0]
and &array
might be easier if I show it a little more "graphically" (with pointers added):
+----------+----------+----------+----------+----------+
| array[0] | array[1] | array[2] | array[3] | array[4] |
+----------+----------+----------+----------+----------+
^
|
&array[0]
|
&array
Things are different with pointers though. The pointer pArray
is pointing to some memory, the value of pArray
is the location of that memory. This is what you get when you use pArray
. It is also the same as &pArray[0]
.
When you use &pArray
you get a pointer to the pointer. That is, you get the location (address) of the variable pArray
itself. Its type is int**
.
Somewhat graphical with the pointer pArray
it would be something like this
+--------+ +-----------+-----------+-----------+-----------+-----------+-----+
| pArray | ----> | pArray[0] | pArray[1] | pArray[2] | pArray[3] | pArray[4] | ... |
+--------+ +-----------+-----------+-----------+-----------+-----------+-----+
^ ^
| |
&pArray &pArray[0]
[Note the ...
at the end of the "array", that's because pointers retains no information about the memory it points to. A pointer is only pointing to a specific location, the "first" element of the "array". Treating the memory as an "array" is up to the programmer.]
6
Excellent ASCII art. Much better than the two hundred words I was about to post.
– molbdnilo
1 hour ago
Not sure that(void*)&array
should be equal to(void*)(&array[0])
BTW. (&array[0]
andarray
should).
– Jarod42
10 mins ago
@Jarod42 An array overlaps exactly with all individual elements of the array. There's no "padding" or "gaps" before, between or after the elements of the array.
– Some programmer dude
2 mins ago
add a comment |
Plain array
decays to a pointer to its first element, it's equal to &array[0]
. The first element also happens to start at the same address as the array itself. Hence &array == &array[0]
.
But it's important to note that the types are different:
- The type of
&array[0]
is (in your example)int*
. - The type of
&array
isint(*)[5]
.
The relationship between &array[0]
and &array
might be easier if I show it a little more "graphically" (with pointers added):
+----------+----------+----------+----------+----------+
| array[0] | array[1] | array[2] | array[3] | array[4] |
+----------+----------+----------+----------+----------+
^
|
&array[0]
|
&array
Things are different with pointers though. The pointer pArray
is pointing to some memory, the value of pArray
is the location of that memory. This is what you get when you use pArray
. It is also the same as &pArray[0]
.
When you use &pArray
you get a pointer to the pointer. That is, you get the location (address) of the variable pArray
itself. Its type is int**
.
Somewhat graphical with the pointer pArray
it would be something like this
+--------+ +-----------+-----------+-----------+-----------+-----------+-----+
| pArray | ----> | pArray[0] | pArray[1] | pArray[2] | pArray[3] | pArray[4] | ... |
+--------+ +-----------+-----------+-----------+-----------+-----------+-----+
^ ^
| |
&pArray &pArray[0]
[Note the ...
at the end of the "array", that's because pointers retains no information about the memory it points to. A pointer is only pointing to a specific location, the "first" element of the "array". Treating the memory as an "array" is up to the programmer.]
Plain array
decays to a pointer to its first element, it's equal to &array[0]
. The first element also happens to start at the same address as the array itself. Hence &array == &array[0]
.
But it's important to note that the types are different:
- The type of
&array[0]
is (in your example)int*
. - The type of
&array
isint(*)[5]
.
The relationship between &array[0]
and &array
might be easier if I show it a little more "graphically" (with pointers added):
+----------+----------+----------+----------+----------+
| array[0] | array[1] | array[2] | array[3] | array[4] |
+----------+----------+----------+----------+----------+
^
|
&array[0]
|
&array
Things are different with pointers though. The pointer pArray
is pointing to some memory, the value of pArray
is the location of that memory. This is what you get when you use pArray
. It is also the same as &pArray[0]
.
When you use &pArray
you get a pointer to the pointer. That is, you get the location (address) of the variable pArray
itself. Its type is int**
.
Somewhat graphical with the pointer pArray
it would be something like this
+--------+ +-----------+-----------+-----------+-----------+-----------+-----+
| pArray | ----> | pArray[0] | pArray[1] | pArray[2] | pArray[3] | pArray[4] | ... |
+--------+ +-----------+-----------+-----------+-----------+-----------+-----+
^ ^
| |
&pArray &pArray[0]
[Note the ...
at the end of the "array", that's because pointers retains no information about the memory it points to. A pointer is only pointing to a specific location, the "first" element of the "array". Treating the memory as an "array" is up to the programmer.]
edited 9 mins ago
answered 1 hour ago
Some programmer dudeSome programmer dude
300k25259424
300k25259424
6
Excellent ASCII art. Much better than the two hundred words I was about to post.
– molbdnilo
1 hour ago
Not sure that(void*)&array
should be equal to(void*)(&array[0])
BTW. (&array[0]
andarray
should).
– Jarod42
10 mins ago
@Jarod42 An array overlaps exactly with all individual elements of the array. There's no "padding" or "gaps" before, between or after the elements of the array.
– Some programmer dude
2 mins ago
add a comment |
6
Excellent ASCII art. Much better than the two hundred words I was about to post.
– molbdnilo
1 hour ago
Not sure that(void*)&array
should be equal to(void*)(&array[0])
BTW. (&array[0]
andarray
should).
– Jarod42
10 mins ago
@Jarod42 An array overlaps exactly with all individual elements of the array. There's no "padding" or "gaps" before, between or after the elements of the array.
– Some programmer dude
2 mins ago
6
6
Excellent ASCII art. Much better than the two hundred words I was about to post.
– molbdnilo
1 hour ago
Excellent ASCII art. Much better than the two hundred words I was about to post.
– molbdnilo
1 hour ago
Not sure that
(void*)&array
should be equal to (void*)(&array[0])
BTW. (&array[0]
and array
should).– Jarod42
10 mins ago
Not sure that
(void*)&array
should be equal to (void*)(&array[0])
BTW. (&array[0]
and array
should).– Jarod42
10 mins ago
@Jarod42 An array overlaps exactly with all individual elements of the array. There's no "padding" or "gaps" before, between or after the elements of the array.
– Some programmer dude
2 mins ago
@Jarod42 An array overlaps exactly with all individual elements of the array. There's no "padding" or "gaps" before, between or after the elements of the array.
– Some programmer dude
2 mins ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54807208%2fwhy-are-array-and-array-pointing-to-the-same-address%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
It's because an array is not a pointer. In the case of an array
&a
anda
are the same thing. In the case of a pointerp
is the address the pointer points to, and&p
is the address where the pointer value is stored.– Jabberwocky
1 hour ago
Here we can witness the inconsistency of the language.
new int[5]
should return a pointer toint[5]
, not a pointer toint
.– geza
1 hour ago
related/duplicate: stackoverflow.com/questions/24104482/…
– geza
1 hour ago