Pointer Arithmetic Practice
Compito
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.
Soluzione
solution
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;
}
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 2. Capitolo 2
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;
}
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione