# pointer question

• 11-04-2009
cardinals03
pointer question
I have the following review question.

A list contains the values 30,40,60 in that order.
p&q are iterators that point to integers on the list.

What will the following print?

Code:

```p = intlist.begin(); p++; q = intlist.insert(p, 20); p = intlist.begin(); p++ cout << *p + *q;```
I think it is 40. p initially starts at 30 then increments to 40. q then inserts 20 as the value. p then goes back to the beginning of the list (30) and increments to the next value (now 20). Am I right? The thing that is hanging me up is wondering whether or not the value of p is updated by p++ or if it stays 30 (then the output would be 50). Thanks
• 11-04-2009
bithub
It would take you about 20 seconds to write a program that would answer your question for you. That's probably less time than it took you to write up this question in the first place.

Quote:

wondering whether or not the value of p is updated by p++ or if it stays 30
p++ moves the iterator to the next position in the list.
• 11-04-2009
cardinals03
Thanks for the asinine reply---I am away from my compiler and was looking over a set of posted review problems. Guess how long it took me to type this???
• 11-04-2009
Elysia
What type is the list?
std::list's iterators are incrementable for some reason, so that code would fail if that was the case.
• 11-04-2009
Daved
>> std::list's iterators are incrementable for some reason, so that code would fail if that was the case.

Did you mean "aren't" instead of "are"? If yes, then I'm not sure why you think that. They are incrementable. If not, then your statement doesn't make sense.
• 11-04-2009
Elysia
"are not"*, is what I meant. But your reply got me thinking. I would expect they should incrementable and the reason I said they were not was because I got an error saying list iterators are not incrementable. But it seems the problem was on my part--I did not actually put any data into the list.
So without further ado, this code:
Code:

```#include <list> #include <iostream> int main() {         std::list<int> intlist;         std::list<int>::iterator p, q;         intlist.push_back(30);         intlist.push_back(40);         intlist.push_back(60);         p = intlist.begin();         p++;         q = intlist.insert(p, 20);         p = intlist.begin();         p++;         std::cout << *p + *q << std::endl; }```
Produces the output: 40.