Array of Pointers Pointing to an unknown location
up vote
2
down vote
favorite
I was practicing with array of pointers.
i wrote a simple code.
#include<iostream>
using namespace std;
int main(){
int a[3]={1,2,3};
int *b[3];
for(int i=0; i<3; i++){
b[i] = &a[i];
}
cout<<b; //This gives a confusing output
cout<<endl;
for(int i=0; i<3; i++){
cout<<b[i];
cout<<endl;
}
}
and Output is
0x6ffe10
0x6ffe30
0x6ffe34
0x6ffe38
Now I don't understand where the b pointing.
I know this sounds weird but i wanna know.
where the b pointer pointing.
The b[0], b[1], b[2] gives expected result.
c++ arrays pointers output cout
add a comment |
up vote
2
down vote
favorite
I was practicing with array of pointers.
i wrote a simple code.
#include<iostream>
using namespace std;
int main(){
int a[3]={1,2,3};
int *b[3];
for(int i=0; i<3; i++){
b[i] = &a[i];
}
cout<<b; //This gives a confusing output
cout<<endl;
for(int i=0; i<3; i++){
cout<<b[i];
cout<<endl;
}
}
and Output is
0x6ffe10
0x6ffe30
0x6ffe34
0x6ffe38
Now I don't understand where the b pointing.
I know this sounds weird but i wanna know.
where the b pointer pointing.
The b[0], b[1], b[2] gives expected result.
c++ arrays pointers output cout
what did you expectb
to be?
– mangusta
Nov 9 at 5:30
It was just an experiment. I searched on Internet to find something about it. but in vain . so i posted it here.
– Shaida Muhammad
Nov 9 at 5:33
so what is the result ofcout<<b
and why do you think it is confusing?
– mangusta
Nov 9 at 5:34
I wanna know what b pointing in Memory. I mean what is inside address b.
– Shaida Muhammad
Nov 9 at 5:35
2
b
is not a pointer, it's an array. Read about arrays decaying into pointers to their first element in your favourite C++ book.
– molbdnilo
Nov 9 at 5:39
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I was practicing with array of pointers.
i wrote a simple code.
#include<iostream>
using namespace std;
int main(){
int a[3]={1,2,3};
int *b[3];
for(int i=0; i<3; i++){
b[i] = &a[i];
}
cout<<b; //This gives a confusing output
cout<<endl;
for(int i=0; i<3; i++){
cout<<b[i];
cout<<endl;
}
}
and Output is
0x6ffe10
0x6ffe30
0x6ffe34
0x6ffe38
Now I don't understand where the b pointing.
I know this sounds weird but i wanna know.
where the b pointer pointing.
The b[0], b[1], b[2] gives expected result.
c++ arrays pointers output cout
I was practicing with array of pointers.
i wrote a simple code.
#include<iostream>
using namespace std;
int main(){
int a[3]={1,2,3};
int *b[3];
for(int i=0; i<3; i++){
b[i] = &a[i];
}
cout<<b; //This gives a confusing output
cout<<endl;
for(int i=0; i<3; i++){
cout<<b[i];
cout<<endl;
}
}
and Output is
0x6ffe10
0x6ffe30
0x6ffe34
0x6ffe38
Now I don't understand where the b pointing.
I know this sounds weird but i wanna know.
where the b pointer pointing.
The b[0], b[1], b[2] gives expected result.
c++ arrays pointers output cout
c++ arrays pointers output cout
asked Nov 9 at 5:24
Shaida Muhammad
609
609
what did you expectb
to be?
– mangusta
Nov 9 at 5:30
It was just an experiment. I searched on Internet to find something about it. but in vain . so i posted it here.
– Shaida Muhammad
Nov 9 at 5:33
so what is the result ofcout<<b
and why do you think it is confusing?
– mangusta
Nov 9 at 5:34
I wanna know what b pointing in Memory. I mean what is inside address b.
– Shaida Muhammad
Nov 9 at 5:35
2
b
is not a pointer, it's an array. Read about arrays decaying into pointers to their first element in your favourite C++ book.
– molbdnilo
Nov 9 at 5:39
add a comment |
what did you expectb
to be?
– mangusta
Nov 9 at 5:30
It was just an experiment. I searched on Internet to find something about it. but in vain . so i posted it here.
– Shaida Muhammad
Nov 9 at 5:33
so what is the result ofcout<<b
and why do you think it is confusing?
– mangusta
Nov 9 at 5:34
I wanna know what b pointing in Memory. I mean what is inside address b.
– Shaida Muhammad
Nov 9 at 5:35
2
b
is not a pointer, it's an array. Read about arrays decaying into pointers to their first element in your favourite C++ book.
– molbdnilo
Nov 9 at 5:39
what did you expect
b
to be?– mangusta
Nov 9 at 5:30
what did you expect
b
to be?– mangusta
Nov 9 at 5:30
It was just an experiment. I searched on Internet to find something about it. but in vain . so i posted it here.
– Shaida Muhammad
Nov 9 at 5:33
It was just an experiment. I searched on Internet to find something about it. but in vain . so i posted it here.
– Shaida Muhammad
Nov 9 at 5:33
so what is the result of
cout<<b
and why do you think it is confusing?– mangusta
Nov 9 at 5:34
so what is the result of
cout<<b
and why do you think it is confusing?– mangusta
Nov 9 at 5:34
I wanna know what b pointing in Memory. I mean what is inside address b.
– Shaida Muhammad
Nov 9 at 5:35
I wanna know what b pointing in Memory. I mean what is inside address b.
– Shaida Muhammad
Nov 9 at 5:35
2
2
b
is not a pointer, it's an array. Read about arrays decaying into pointers to their first element in your favourite C++ book.– molbdnilo
Nov 9 at 5:39
b
is not a pointer, it's an array. Read about arrays decaying into pointers to their first element in your favourite C++ book.– molbdnilo
Nov 9 at 5:39
add a comment |
4 Answers
4
active
oldest
votes
up vote
4
down vote
accepted
b
is an array of pointers to int
. It's address is the address of the first pointer.
- The first pointer
b[0]
is assigned the address of elementa[0]
(sob[0]
points to1
).
b[1]
is assigned the address ofa[1]
(sob[1]
points to2
)
b[2]
is assigned the address ofa[2]
(sob[2]
points to3
)
In each case the pointer in the b
array holds the address of the corresponding element within a
. So if there are any changes made to the elements of a
after your loop assigning the address in a
to b
, the value held in the memory location pointed to by the element of b
will change, but the address for that value held by b
will remain unchanged.
add a comment |
up vote
3
down vote
When b
is passed to cout
's <<
operator, it decays into a pointer; hence as far as the printing function is concerned, printing out b
is the same as printing out &b[0]
.
Therefore, cout << b;
prints out the address of the first pointer in the b[3]
array. (Note that the address of the pointer is not the same thing as the address the pointer is pointing to! That can be a source of confusion -- just keep in mind that a pointer-variable is a variable as well, and like any other variable, it has its own unique location in memory, which is distinct from the location it is pointing at)
It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
– alter igel
Nov 9 at 5:49
Good point, I'll update my answer :)
– Jeremy Friesner
Nov 9 at 5:50
add a comment |
up vote
3
down vote
b
is an array of pointers to int
which is a local variable on the stack.
Its address does not depend on what individual elements of its array are assigned to.
If you print its value before the for
loop (where you assign values to its members), you will see that it is the same as the value printed after the for
loop.
See live demo here.
Pictorially:
add a comment |
up vote
2
down vote
b Memory adress of the whole array.
I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
– Shaida Muhammad
Nov 9 at 5:42
If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
– Allamo Olsson
Nov 9 at 5:44
Don't forget there are two arrays in the program;a
andb
. Thea
array starts at0x6ffe30
and is 12 bytes long (3 4-byte ints), whereas theb
array starts at0x6ffe10
and is 24 bytes long (3 8-byte pointers)
– Jeremy Friesner
Nov 9 at 5:59
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
b
is an array of pointers to int
. It's address is the address of the first pointer.
- The first pointer
b[0]
is assigned the address of elementa[0]
(sob[0]
points to1
).
b[1]
is assigned the address ofa[1]
(sob[1]
points to2
)
b[2]
is assigned the address ofa[2]
(sob[2]
points to3
)
In each case the pointer in the b
array holds the address of the corresponding element within a
. So if there are any changes made to the elements of a
after your loop assigning the address in a
to b
, the value held in the memory location pointed to by the element of b
will change, but the address for that value held by b
will remain unchanged.
add a comment |
up vote
4
down vote
accepted
b
is an array of pointers to int
. It's address is the address of the first pointer.
- The first pointer
b[0]
is assigned the address of elementa[0]
(sob[0]
points to1
).
b[1]
is assigned the address ofa[1]
(sob[1]
points to2
)
b[2]
is assigned the address ofa[2]
(sob[2]
points to3
)
In each case the pointer in the b
array holds the address of the corresponding element within a
. So if there are any changes made to the elements of a
after your loop assigning the address in a
to b
, the value held in the memory location pointed to by the element of b
will change, but the address for that value held by b
will remain unchanged.
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
b
is an array of pointers to int
. It's address is the address of the first pointer.
- The first pointer
b[0]
is assigned the address of elementa[0]
(sob[0]
points to1
).
b[1]
is assigned the address ofa[1]
(sob[1]
points to2
)
b[2]
is assigned the address ofa[2]
(sob[2]
points to3
)
In each case the pointer in the b
array holds the address of the corresponding element within a
. So if there are any changes made to the elements of a
after your loop assigning the address in a
to b
, the value held in the memory location pointed to by the element of b
will change, but the address for that value held by b
will remain unchanged.
b
is an array of pointers to int
. It's address is the address of the first pointer.
- The first pointer
b[0]
is assigned the address of elementa[0]
(sob[0]
points to1
).
b[1]
is assigned the address ofa[1]
(sob[1]
points to2
)
b[2]
is assigned the address ofa[2]
(sob[2]
points to3
)
In each case the pointer in the b
array holds the address of the corresponding element within a
. So if there are any changes made to the elements of a
after your loop assigning the address in a
to b
, the value held in the memory location pointed to by the element of b
will change, but the address for that value held by b
will remain unchanged.
answered Nov 9 at 5:47
David C. Rankin
38.9k32546
38.9k32546
add a comment |
add a comment |
up vote
3
down vote
When b
is passed to cout
's <<
operator, it decays into a pointer; hence as far as the printing function is concerned, printing out b
is the same as printing out &b[0]
.
Therefore, cout << b;
prints out the address of the first pointer in the b[3]
array. (Note that the address of the pointer is not the same thing as the address the pointer is pointing to! That can be a source of confusion -- just keep in mind that a pointer-variable is a variable as well, and like any other variable, it has its own unique location in memory, which is distinct from the location it is pointing at)
It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
– alter igel
Nov 9 at 5:49
Good point, I'll update my answer :)
– Jeremy Friesner
Nov 9 at 5:50
add a comment |
up vote
3
down vote
When b
is passed to cout
's <<
operator, it decays into a pointer; hence as far as the printing function is concerned, printing out b
is the same as printing out &b[0]
.
Therefore, cout << b;
prints out the address of the first pointer in the b[3]
array. (Note that the address of the pointer is not the same thing as the address the pointer is pointing to! That can be a source of confusion -- just keep in mind that a pointer-variable is a variable as well, and like any other variable, it has its own unique location in memory, which is distinct from the location it is pointing at)
It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
– alter igel
Nov 9 at 5:49
Good point, I'll update my answer :)
– Jeremy Friesner
Nov 9 at 5:50
add a comment |
up vote
3
down vote
up vote
3
down vote
When b
is passed to cout
's <<
operator, it decays into a pointer; hence as far as the printing function is concerned, printing out b
is the same as printing out &b[0]
.
Therefore, cout << b;
prints out the address of the first pointer in the b[3]
array. (Note that the address of the pointer is not the same thing as the address the pointer is pointing to! That can be a source of confusion -- just keep in mind that a pointer-variable is a variable as well, and like any other variable, it has its own unique location in memory, which is distinct from the location it is pointing at)
When b
is passed to cout
's <<
operator, it decays into a pointer; hence as far as the printing function is concerned, printing out b
is the same as printing out &b[0]
.
Therefore, cout << b;
prints out the address of the first pointer in the b[3]
array. (Note that the address of the pointer is not the same thing as the address the pointer is pointing to! That can be a source of confusion -- just keep in mind that a pointer-variable is a variable as well, and like any other variable, it has its own unique location in memory, which is distinct from the location it is pointing at)
edited Nov 9 at 5:57
answered Nov 9 at 5:46
Jeremy Friesner
37.8k1077157
37.8k1077157
It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
– alter igel
Nov 9 at 5:49
Good point, I'll update my answer :)
– Jeremy Friesner
Nov 9 at 5:50
add a comment |
It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
– alter igel
Nov 9 at 5:49
Good point, I'll update my answer :)
– Jeremy Friesner
Nov 9 at 5:50
It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
– alter igel
Nov 9 at 5:49
It may be logically equivalent in the context of pointer decay, but generally arrays are distinct types from pointers
– alter igel
Nov 9 at 5:49
Good point, I'll update my answer :)
– Jeremy Friesner
Nov 9 at 5:50
Good point, I'll update my answer :)
– Jeremy Friesner
Nov 9 at 5:50
add a comment |
up vote
3
down vote
b
is an array of pointers to int
which is a local variable on the stack.
Its address does not depend on what individual elements of its array are assigned to.
If you print its value before the for
loop (where you assign values to its members), you will see that it is the same as the value printed after the for
loop.
See live demo here.
Pictorially:
add a comment |
up vote
3
down vote
b
is an array of pointers to int
which is a local variable on the stack.
Its address does not depend on what individual elements of its array are assigned to.
If you print its value before the for
loop (where you assign values to its members), you will see that it is the same as the value printed after the for
loop.
See live demo here.
Pictorially:
add a comment |
up vote
3
down vote
up vote
3
down vote
b
is an array of pointers to int
which is a local variable on the stack.
Its address does not depend on what individual elements of its array are assigned to.
If you print its value before the for
loop (where you assign values to its members), you will see that it is the same as the value printed after the for
loop.
See live demo here.
Pictorially:
b
is an array of pointers to int
which is a local variable on the stack.
Its address does not depend on what individual elements of its array are assigned to.
If you print its value before the for
loop (where you assign values to its members), you will see that it is the same as the value printed after the for
loop.
See live demo here.
Pictorially:
edited Nov 9 at 8:02
answered Nov 9 at 5:34
P.W
7,6802438
7,6802438
add a comment |
add a comment |
up vote
2
down vote
b Memory adress of the whole array.
I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
– Shaida Muhammad
Nov 9 at 5:42
If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
– Allamo Olsson
Nov 9 at 5:44
Don't forget there are two arrays in the program;a
andb
. Thea
array starts at0x6ffe30
and is 12 bytes long (3 4-byte ints), whereas theb
array starts at0x6ffe10
and is 24 bytes long (3 8-byte pointers)
– Jeremy Friesner
Nov 9 at 5:59
add a comment |
up vote
2
down vote
b Memory adress of the whole array.
I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
– Shaida Muhammad
Nov 9 at 5:42
If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
– Allamo Olsson
Nov 9 at 5:44
Don't forget there are two arrays in the program;a
andb
. Thea
array starts at0x6ffe30
and is 12 bytes long (3 4-byte ints), whereas theb
array starts at0x6ffe10
and is 24 bytes long (3 8-byte pointers)
– Jeremy Friesner
Nov 9 at 5:59
add a comment |
up vote
2
down vote
up vote
2
down vote
b Memory adress of the whole array.
b Memory adress of the whole array.
answered Nov 9 at 5:40
Allamo Olsson
849
849
I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
– Shaida Muhammad
Nov 9 at 5:42
If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
– Allamo Olsson
Nov 9 at 5:44
Don't forget there are two arrays in the program;a
andb
. Thea
array starts at0x6ffe30
and is 12 bytes long (3 4-byte ints), whereas theb
array starts at0x6ffe10
and is 24 bytes long (3 8-byte pointers)
– Jeremy Friesner
Nov 9 at 5:59
add a comment |
I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
– Shaida Muhammad
Nov 9 at 5:42
If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
– Allamo Olsson
Nov 9 at 5:44
Don't forget there are two arrays in the program;a
andb
. Thea
array starts at0x6ffe30
and is 12 bytes long (3 4-byte ints), whereas theb
array starts at0x6ffe10
and is 24 bytes long (3 8-byte pointers)
– Jeremy Friesner
Nov 9 at 5:59
I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
– Shaida Muhammad
Nov 9 at 5:42
I guess the address of array is the 0 index of array . but this address is 20 bytes ahead of that . . .
– Shaida Muhammad
Nov 9 at 5:42
If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
– Allamo Olsson
Nov 9 at 5:44
If you look at the size of b you will see that it will be the size of the 3 integers. It is the whole container. (And yes i understand what you mean... )
– Allamo Olsson
Nov 9 at 5:44
Don't forget there are two arrays in the program;
a
and b
. The a
array starts at 0x6ffe30
and is 12 bytes long (3 4-byte ints), whereas the b
array starts at 0x6ffe10
and is 24 bytes long (3 8-byte pointers)– Jeremy Friesner
Nov 9 at 5:59
Don't forget there are two arrays in the program;
a
and b
. The a
array starts at 0x6ffe30
and is 12 bytes long (3 4-byte ints), whereas the b
array starts at 0x6ffe10
and is 24 bytes long (3 8-byte pointers)– Jeremy Friesner
Nov 9 at 5:59
add a comment |
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%2f53220305%2farray-of-pointers-pointing-to-an-unknown-location%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
what did you expect
b
to be?– mangusta
Nov 9 at 5:30
It was just an experiment. I searched on Internet to find something about it. but in vain . so i posted it here.
– Shaida Muhammad
Nov 9 at 5:33
so what is the result of
cout<<b
and why do you think it is confusing?– mangusta
Nov 9 at 5:34
I wanna know what b pointing in Memory. I mean what is inside address b.
– Shaida Muhammad
Nov 9 at 5:35
2
b
is not a pointer, it's an array. Read about arrays decaying into pointers to their first element in your favourite C++ book.– molbdnilo
Nov 9 at 5:39