Pointer Arithmetic Practice
Uppgift
Swipe to start coding
- Create a pointer that points to the first variable.
- Find the distance between memory locations of this two variables.
- Using the pointer arithmetic move the pointer to the location of the second variable.
- Modify the value of dereferenced pointer to
50
and output the second variable.
Lösning
solution.cpp
99
1
2
3
4
5
6
7
8
9
10
11
12
13
#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?
Tack för dina kommentarer!
Avsnitt 2. Kapitel 2
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
int main()
{
int firstVariable = 10;
int secondVariable = 20;
int _ pointer = _ ___;
pointer += (&___ - ___);
_(pointer) = 50;
std::cout << secondVariable << std::endl;
}
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal