FAQ: References and Pointers - Memory Address

This community-built FAQ covers the “Memory Address” exercise from the lesson “References and Pointers”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Memory Address

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

If I consider the following code -

int soda = 99;
int &pop = soda;
pop += 1;
soda +=  20;
std::cout << soda << "\t" << pop;

This one changes the value to 120, so a change to a reference changes the original variable and vice-versa, but in Line 2 it’s not a declaration, though it’s not being considered as the Address-Of operator , WHY ? Am I having some wrong concepts about Declaration ?

1 Like

First, remember this point:
If & is used in declaration, it’s a reference, else it’s a memory address.

In your example, & is used in declaration. Therefore, it’s a reference (aka alias). Reference is directly modifying the variable it’s pointing to. Hence, line 3 and 4 of your code will make changes on soda and pop, and both of them are the same. Line 3, adds 1 to initial value (99+1=100) and line 4 adds 20 (100+20=120).

Line 2 of your code IS A DECLARATION.

  • When & is used in a declaration, it is a reference operator.
  • When & is not used in a declaration, it is an address operator.

When it is not used in a declaration where else can it be used??? :slight_smile:

A minor typo report:

So we haved learned…

should be just “So we have learned…”