cplusplus:pointers_1
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cplusplus:pointers_1 [2018/02/24 20:50] – [Pointers in the abstract] mithat | cplusplus:pointers_1 [2019/03/28 16:24] (current) – [Pointer operators] mithat | ||
---|---|---|---|
Line 11: | Line 11: | ||
===== Pointer variables ===== | ===== Pointer variables ===== | ||
- | You can think of a **pointer variable** as a variable that stores the address of some other variable. | + | You can think of a **pointer variable** as a variable that stores the base address of some other variable. |
And? | And? | ||
Line 30: | Line 30: | ||
If we were now to ask what the address of '' | If we were now to ask what the address of '' | ||
- | So, now let's say that (for whatever reason) we wanted to create a variable to store the address where '' | + | So, now let's say that (for whatever reason) we wanted to create a variable to store the address where '' |
^ Variable name ^ Memory location ^ Memory contents across all 8 bytes (64 bits) ^ | ^ Variable name ^ Memory location ^ Memory contents across all 8 bytes (64 bits) ^ | ||
Line 133: | Line 133: | ||
Pointer variables in C++ are not automatically initialized. This means that a pointer variable declaration along the lines of | Pointer variables in C++ are not automatically initialized. This means that a pointer variable declaration along the lines of | ||
<code cpp> | <code cpp> | ||
- | leaves the pointer pointing to an arbitrary memory location. This is dangerous because poking your fingers into arbitrary memory locations is a good way to corrupt your program' | + | leaves the pointer pointing to an arbitrary memory location. This is dangerous because poking your fingers into arbitrary memory locations is a good way to corrupt your program' |
Pointer variables can be initialized when declared. It is good programming practice to always initialize pointers so they do not accidentally point to unknown memory locations. The code below initializes the value of the pointer variable in the declaration: | Pointer variables can be initialized when declared. It is good programming practice to always initialize pointers so they do not accidentally point to unknown memory locations. The code below initializes the value of the pointer variable in the declaration: | ||
Line 162: | Line 162: | ||
=== Indirection/ | === Indirection/ | ||
- | The **indirection** or **dereferencing** operator, '' | + | The **indirection** or **dereferencing** operator, '' |
<code cpp> | <code cpp> | ||
int y = -1; // declare y and initialize its value | int y = -1; // declare y and initialize its value | ||
Line 177: | Line 177: | ||
cout << y; // prints 7</ | cout << y; // prints 7</ | ||
- | Think of the indirection/ | + | You can think of the indirection/ |
- | < | + | < |
The '' | The '' |
cplusplus/pointers_1.1519505410.txt.gz · Last modified: 2018/02/24 20:50 by mithat