-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdding an integer to a pointer.cpp
More file actions
50 lines (32 loc) · 1.09 KB
/
Adding an integer to a pointer.cpp
File metadata and controls
50 lines (32 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;
// byte = 1 byte
// car = 1 byte
// short = 2 byte
// int = 4 byte
// long = 8 byte
// float = 4 byte
// double = 8 byte
int main()
{
setlocale(LC_ALL, "italian");
int array[] = { 10, 20, 30 };
int *pa = array;
cout << "pa = " << *pa << endl;
pa += 1; // in questo modo ci spostiamo da un indice ad altro, dato che distano 4 bytes (int), il nostro indirizzo fara +4
cout << "pa += 1 = " << *pa << endl;
pa += 1; // bisigna fare molta attenzione di non uscire fuori dalla "memoria a noi disponibile"
cout << "pa += 1 = " << *pa << endl;
cout << "\n--------------\n" << endl;
short s;
short *ps = &s;
char c;
char *pc = &c;
cout << "Prima ps = " << ps << endl;
ps += 1; // == ps = ps + 1
cout << "Dopo ps += 1 = " << ps << endl; // deve aumentare di due dato che short è di due byte
cout << "Prima pc = " << pc << endl; // diciamo che non mi funziona probabilmente perchè sono sul mac.
pc = pc + 1;
cout << "Dopo pc += 1 = " << pc << endl; // dovrebbe aumentare di uno da che char è di un byte
return 0;
}