Reputation: 41
Ok so I have some code from a study guide and I put it into a compiler and I don't completely understand the result.
#include <iostream>
using namespace std;
int main()
{
int *p, *p1;
p = new int[10];
p1 = p++;
for (int i=0; i<10;i++)
{
*p1 = i*10;
p1++;
}
for (int i=0; i<10;i+=2)
{
p[i] = i*100;
}
for (int i=0; i<5;i++)
{
cout << *p++ << " ";
}
}
I basically think I understand everything except for the line that reads p1 = p++;
I think that's just saying that p1
now points to the same array as p
but I don't exactly know what the p++
part of it does.
When I put it in a compiler I get 0 20 200 40 400
which I don't understand. Can someone explain what that one line means and then why I'm getting that output?
Upvotes: 4
Views: 96
Reputation: 595827
I think that's just saying that
p1
now points to the same array asp
.
Yes, that is correct.
I don't exactly know what the
p++
part of it does.
It increments p
to point at the next element in the array. The post-increment operator is being used, so the original value of p
prior to the increment gets returned, which is why p1
points at the 1st element and not the 2nd element.
Can someone explain ... why I'm getting that output?
On the 1st loop, p1
points at the 1st array element, and is incremented to each subsequent element on each loop iteration. The loop sets each array value to i*10
, thus the array contains 0 10 20 30 40 50 60 70 80 90
:
array[0] = 0*10 = 0
array[1] = 1*10 = 10
array[2] = 2*10 = 20
array[3] = 3*10 = 30
array[4] = 4*10 = 40
array[5] = 5*10 = 50
array[6] = 6*10 = 60
array[7] = 7*10 = 70
array[8] = 8*10 = 80
array[9] = 9*10 = 90
On the 2nd loop, p
points at the 2nd array element, and the loop skips past every other element on each loop iteration. The loop sets just those array elements to i*100
, thus the array contains 0 0 20 200 40 400 60 600 80 800
:
array[0] = 0
array[1] = 0*100 = 0
array[2] = 20
array[3] = 2*100 = 200
array[4] = 40
array[5] = 4*100 = 400
array[6] = 60
array[7] = 6*100 = 600
array[8] = 80
array[9] = 8*100 = 800
On the 3rd loop, p
still points at the 2nd array element. The loop simply outputs 5 consecutive elements, thus printing 0 20 200 40 400
.
Upvotes: 2
Reputation: 17890
p = new int[10];
We allocate an array with space for 10 ints, and point p
at its head:
p-> 0:[ - ]
1:[ - ]
2:[ - ]
3:[ - ]
4:[ - ]
5:[ - ]
6:[ - ]
7:[ - ]
8:[ - ]
9:[ - ]
p1 = p++;
This seems a bit odd and isn't really good style. This copies the value of p into p1 (so it points where p1 was pointing) and then increments p to point to the next item in the array. It's generally a bad idea not to retain a pointer to the start of a dynamically allocated array, because it means you can't deallocate it. (In principle you can of course decrement the pointer back to the start, but to do that you need to know how many places to decrement, and it's a lot easier just to keep the original pointer. And if you think this is a lot of hassle, it is! That's why std::array and std::vector exist. But let's return to the question at hand...)
0:[ - ] <- p1
p-> 1:[ - ]
2:[ - ]
3:[ - ]
4:[ - ]
5:[ - ]
6:[ - ]
7:[ - ]
8:[ - ]
9:[ - ]
for (int i=0; i<10;i++)
{
*p1 = i*10;
p1++;
}
This loops ten times, and each time it does, it sets the value in the array pointed at by p1, and advances p1 one place. By the end, p1 is pointing one position past the end of the array. (That's okay, but only so long as nothing tries to read or write to that position.)
0:[ 0 ]
p-> 1:[ 10 ]
2:[ 20 ]
3:[ 30 ]
4:[ 40 ]
5:[ 50 ]
6:[ 60 ]
7:[ 70 ]
8:[ 80 ]
9:[ 90 ]
<- p1
for (int i=0; i<10;i+=2)
{
p[i] = i*100;
}
This is a bit more confusing. Remember that p is pointing at index #1 of the original array, not index #0. This loops five times, with i having values of 0, 2, 4, 6 and 8. Since p is already pointing at index #1 instead of #0, this actually updates indexes #1, #3, #5, #7 and #9 in the original array.
0:[ 0 ]
p-> 1:[ 0 ]
2:[ 20 ]
3:[ 200 ]
4:[ 40 ]
5:[ 400 ]
6:[ 60 ]
7:[ 600 ]
8:[ 80 ]
9:[ 800 ]
<- p1
for (int i=0; i<5;i++)
{
cout << *p++ << " ";
}
Finally this loops five times, each time outputting the element currently pointed at by p while also moving p to the next item.
0:[ 0 ]
1:[ 0 ] <
2:[ 20 ] <
3:[ 200 ] < These five elements were output.
4:[ 40 ] <
5:[ 400 ] <
p-> 6:[ 60 ]
7:[ 600 ]
8:[ 80 ]
9:[ 800 ]
<- p1
Upvotes: 8
Reputation: 81926
In addition to the other answer, perhaps this is helpful.
int *p = new int[10];
int *p1 = p++;
// P1 P
// | |
// v v
// +-------------------------------------------------+
// | | | | | | | | | | |
// +-------------------------------------------------+
for (int i=0; i<10;i++) {
*p1 = i*10;
p1++;
}
// P P1
// | |
// v v
//+-------------------------------------------------+
//| 0| 10| 20| 30| 40| 50| 60| 70| 80| 90|
//+-------------------------------------------------+
for (int i=0; i<10;i+=2) {
p[i] = i*100;
}
// P P1
// | |
// v v
// +-------------------------------------------------+
// | 0| 0| 20| 200| 40| 400| 60| 600| 80| 800|
// +-------------------------------------------------+
Upvotes: 3
Reputation: 2789
p1 = p++;
is using the post-increment operator (++
as suffix) which is going to:
p1
the current value of p
(or more accurately returns the value of p
before it is incremented)p
the value p+1
. And because p
is an int*
, it is going to point to the next int
.So to your point...
I think that's just saying that p1 now points to the same array as p
p1
is actually now pointing to the first element of the array previously created, which is also the base address of the array yes. And p
now points to the 2nd element of that array.
Upvotes: 4