Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Pointer Arithmetic | Pointer Arithmetic
C++ Pointers and References

bookChallenge: Pointer Arithmetic

Task

Swipe to start coding

Sometimes in programming, you may want to access the value of a variable indirectly using a pointer.

In this task, you will write a function that starts with one variable, moves a pointer to another variable, and returns the value stored there.

  1. Calculate the distance between the two pointers by subtracting the address of p_start from the address of p_target.
  2. Add this difference to the p_start pointer. This gives you a pointer to the value stored in p_target.
  3. Return the value pointed to by p_start.

Solution

solution.cpp

solution.cpp

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain this in simpler terms?

What are some examples related to this topic?

Where can I learn more about this?

close

Awesome!

Completion rate improved to 5.88

bookChallenge: Pointer Arithmetic

Swipe to show menu

Task

Swipe to start coding

Sometimes in programming, you may want to access the value of a variable indirectly using a pointer.

In this task, you will write a function that starts with one variable, moves a pointer to another variable, and returns the value stored there.

  1. Calculate the distance between the two pointers by subtracting the address of p_start from the address of p_target.
  2. Add this difference to the p_start pointer. This gives you a pointer to the value stored in p_target.
  3. Return the value pointed to by p_start.

Solution

solution.cpp

solution.cpp

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
single

single

some-alt