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

book
Pointer Arithmetic Practice

Uppgift

Swipe to start coding

  1. Create a pointer that points to the first variable.
  2. Find the distance between memory locations of this two variables.
  3. Using the pointer arithmetic move the pointer to the location of the second variable.
  4. Modify the value of dereferenced pointer to 50 and output the second variable.

Lösning

solution.cpp

solution.cpp

#include <iostream>

int main()
{
int firstVariable = 10;
int secondVariable = 20;
int *pointer = &firstVariable;
pointer += &secondVariable - pointer;
*pointer = 50;
std::cout << secondVariable << std::endl;
}

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2
single

single

#include <iostream>

int main()
{
int firstVariable = 10;
int secondVariable = 20;
int _ pointer = _ ___;

pointer += (&___ - ___);
_(pointer) = 50;
std::cout << secondVariable << std::endl;
}

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt