cplusplus:pointers_1
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
cplusplus:pointers_1 [2016/03/05 22:05] – [Pointers 1] mithat | cplusplus:pointers_1 [2018/02/24 23:53] – [Pointer variables] 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 50: | Line 50: | ||
{{ : | {{ : | ||
- | In this diagram, the //value// of the pointer '' | + | In this diagram, the //value// of the pointer '' |
Just as the value of a normal variable can change, the value of a pointer can change as well. Let’s say in addition to the variable '' | Just as the value of a normal variable can change, the value of a pointer can change as well. Let’s say in addition to the variable '' | ||
Line 76: | Line 76: | ||
<code cpp>int* myPtr, anotherOne; | <code cpp>int* myPtr, anotherOne; | ||
- | Pointers can be created to point to any type, but they //must// point to some type: | + | Pointers can be created to point to any type: |
<code cpp> | <code cpp> | ||
char *yourPtr; | char *yourPtr; | ||
- | double *herPtr; // declare a pointer to a double | + | double *zerPtr; // declare a pointer to a double |
- | string *hisPtr; // declare a pointer to a string</ | + | string *hyPtr; // declare a pointer to a string |
+ | </ | ||
=== Assignment === | === Assignment === | ||
Line 86: | Line 87: | ||
<code cpp>int num = -42; | <code cpp>int num = -42; | ||
cout << num << endl; // prints value held in variable num | cout << num << endl; // prints value held in variable num | ||
- | cout << &num << endl ; // prints the base address of variable num</ | + | cout << &num << endl ; // prints the base address of variable num |
+ | </ | ||
Most environments show base addresses as [[http:// | Most environments show base addresses as [[http:// | ||
Line 93: | Line 95: | ||
int y = 5; // declare an integer variable y | int y = 5; // declare an integer variable y | ||
int *myPtr; | int *myPtr; | ||
- | myPtr = & | + | myPtr = & |
+ | </ | ||
The result of this code fragment may be diagrammed as follows: | The result of this code fragment may be diagrammed as follows: | ||
Line 124: | Line 127: | ||
myPtr = & | myPtr = & | ||
- | myPtr = & | + | myPtr = & |
+ | </ | ||
=== Initialization === | === Initialization === | ||
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 | + | leaves the pointer pointing to an arbitrary memory location. This is dangerous because poking your fingers into arbitrary memory locations |
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: | ||
<code cpp> | <code cpp> | ||
int y = 5; | int y = 5; | ||
- | int *myPtr = & | + | int *myPtr = & |
+ | </ | ||
==== nullptr/ | ==== nullptr/ | ||
Line 172: | Line 177: | ||
cout << y; // prints 7</ | cout << y; // prints 7</ | ||
- | Think of the indirection/ | + | You can think of the indirection/ |
- | < | + | < |
The '' | The '' | ||
Line 190: | Line 195: | ||
int main() | int main() | ||
{ | { | ||
- | int a; | + | int a; // a is an integer |
- | int *aPtr = NULL; // aPtr is a pointer to an integer | + | int *aPtr = nullptr; // aPtr is a pointer to an integer |
- | + | ||
- | a = 7; | + | a = 7; // give a a value |
- | aPtr = & | + | aPtr = & |
+ | |||
cout << "The value of a is: " | cout << "The value of a is: " | ||
<< | << | ||
<< | << | ||
+ | |||
cout << endl; | cout << endl; | ||
+ | |||
cout << "The value of a is: " | cout << "The value of a is: " | ||
<< | << | ||
cout << endl; | cout << endl; | ||
+ | |||
cout << " | cout << " | ||
<< | << | ||
Line 211: | Line 216: | ||
<< | << | ||
return 0; | return 0; | ||
- | }</ | + | } |
+ | </ | ||
cplusplus/pointers_1.txt · Last modified: 2019/03/28 16:24 by mithat