Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct answer for "Jumping into C++" chapter 13, quiz 3?

Tags:

c++

Which of the following gives the memory address of a variable pointed to by pointer p_a?

A. p_a;

B. *p_a;

C. &p_a;

D. address( p_a );

So I'm reading this book called "Jumping into C++" and this quiz came up after one chapter. When I checked the right answer from the book it showed that the correct answer was C. &p_a;. Wouldn't &p_a just give the memory address of a pointer variable p_a instead of memory address of variable it is pointing to?

Now I'd say that this is not the correct answer, but I can't be sure. After all I'm still a beginner so I don't dare to start questioning answers in the book just yet. I'd say that the correct answer is A. Tell me, is there a mistake in the answers or what?

like image 856
xtpro Avatar asked Jan 12 '23 06:01

xtpro


2 Answers

p_a is a pointer pointing to a variable. That is, it yields the address of that variable.

*p_a is the value of the variable.

&p_a is the address of the pointer (pointers have memory locations too).

address( p_a ) is some nonsense.

Hence the answer is A and the book is incorrect. Burn it.

like image 99
Bathsheba Avatar answered Jan 13 '23 20:01

Bathsheba


Unless I am totally misunderstanding the question, you are right and the book is wrong. p_a is a pointer, meaning it is the address of the variable it points to.

like image 28
Mad Physicist Avatar answered Jan 13 '23 18:01

Mad Physicist