Pointers assignment

I need some help to understand pointers. Our teacher has given us an assignment which just doesn't make sense to me.

The task is:
First make a variable 'number' which has value 42.
Second to make a pointer 'p' and make it point to 'number'.
Then the value of 'number' is to be printed by using 'p'.

After that the value of 'number' is to be increased by 1 by using 'p' and then printing by using 'number'.
Then reduce the value of 'number' by 1 using 'number' and then print it by using 'p'.
Lastly 'p' has to be printed, but it cannot have the value 42.

How is this even possible? It sounds to me like the task is contradicting itself.

1
2
3
4
5
6
7
8
9
10
11
12
13
int number = 42;

int *p = &number;

cout << "The number has value " << *p << "\n";

++(*p);

cout << "The number is " << number << "\n";

number -=1;

cout << "The number is " << *p;
Lastly 'p' has to be printed, but it cannot have the value 42.

Clarify this statement please. Does this mean the address 'p' has needs to be printed?
 
std::cout << p;


Output
0x28ff28


or does it mean that the value that p is pointing to needs to be printed, which is what you did?
Last edited on
You are doing fine.

For the last one: what is the difference between these two:
the value of 'number' is to be printed by using 'p'
the value of 'p' is to be printed
mpark4656:
I have no idea. I've described everything the task asks for, I'm afraid I don't know. I think it means the value, but I'm not sure.

keskiverto:
I don't think there is a difference, but that's how the task was given. It has to be noted that the teacher is a bit of an oddball, so she does some stuff that doesn't make sense.
Hello Anonymousandro,

As I interpret the last instruction
Lastly 'p' has to be printed, but it cannot have the value 42
it says to me that the value of number has to be changed to something other than 42 before it is printed out. I also take this to mena that you have to use p to print the last line.

I believe that you code has captured the requirement correctly.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.