Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Reference or Pointer?

References and pointers both let you work with a variable from somewhere else, such as from a function. But they differ in a few ways:

ReferencePointer
Creatingint& r = x;int* p = &x;
Valuer, like an ordinary variable*p
Function calladdPoint(score)addPoint(&score)
"Nothing"not possible, a reference always belongs to a variablenullptr, check it before use
Another variablenot possible, a reference always belongs to the same onep = &y;

So when should you use which? Prefer references:

  • The function should change a variable or a vector: a reference, int& score, std::vector<int>& scores.
  • The function only reads a vector or text: a const reference, const std::vector<int>& numbers, const std::string& name.
  • Use a pointer only when you need "nothing" (nullptr) or to redirect it to another variable, or when a library you use requires it.

References are simpler and safer: you don't write asterisks, you can't forget the & in a call, and a reference can't be nullptr, so you don't have to check it. But pointers stay important in C++. You will need them in more advanced topics, for example when a program asks for new memory while it runs, or when you build your own data structures.

Instructions

  1. 1

    The program prints Hi!Hi!Hi!, but both functions use pointers for no good reason. Rewrite the function addExclamation so that it takes a reference std::string& instead of a pointer. You then won't need * inside the function or & in the call.

  2. 2

    Rewrite the function repeat the same way. The program still prints Hi!Hi!Hi!.

  3. 3

    Below the function repeat, write a function wrap with two parameters of type std::string: the text, which the function changes, and the border, which it only reads. Use the rules above to choose how to pass them. The function adds the border to the beginning and to the end of the text. Before the print line, call wrap(greeting, "**"). The program prints **Hi!Hi!Hi!**. If you passed the border by reference without const, compiling would end with the error no matching function for call to 'wrap': text in quotes isn't a variable, so a plain reference doesn't accept it.

Back

9/10

  1. 1. Passing by Value and by Reference
  2. 2. Swapping Two Variables
  3. 3. Changing a Vector in a Function
  4. 4. Const References
  5. 5. Addresses and Pointers
  6. 6. The & and * Signs
  7. 7. Pointers as Parameters
  8. 8. The Null Pointer
  9. 9. Reference or Pointer?
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.