-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointersstruc.cpp
More file actions
46 lines (29 loc) · 760 Bytes
/
pointersstruc.cpp
File metadata and controls
46 lines (29 loc) · 760 Bytes
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
#include <iostream>
using namespace std;
struct Container
{
string Name;
int x;
int y;
int z;
};
int main()
{
int numbers[] = { 0,1,2,3,4,5,6,7,9,10 };
int* NumPtr = numbers; //name of the array is the address of the first element (so it is a pointer!)
cout << NumPtr << endl;
cout << *NumPtr << endl;
// use pointer arithmetic
NumPtr++; // move it to the next element in the array
cout << NumPtr << endl;
cout << *NumPtr << endl;
NumPtr += 3; // Pointer arithmetic not regular!
cout << NumPtr << endl;
cout << *NumPtr << endl;
Container container = { "Sam",5,6,7 };
Container* PtrToCont = &container;
cout << (*PtrToCont).Name << endl;
cout << (*PtrToCont).x << endl;
// syntactical sugar
cout << PtrToCont->Name << endl;
}