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 [2015/12/31 20:51] – mithat | cplusplus:pointers_1 [2019/03/28 16:24] (current) – [Pointer operators] mithat | ||
---|---|---|---|
Line 2: | Line 2: | ||
Pointer fundamentals, | Pointer fundamentals, | ||
Deitel, Harvey M., and Paul J. Deitel. " | Deitel, Harvey M., and Paul J. Deitel. " | ||
- | |||
- | <WRAP center round important 60%> | ||
- | This content has not been vetted for C++11 compliance. | ||
- | </ | ||
Line 15: | 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 34: | 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 54: | 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 80: | 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 90: | 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 97: | 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 128: | 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 = & |
+ | </ | ||
- | ==== NULL pointers ==== | + | ==== nullptr/NULL pointers ==== |
- | You can set a pointer to a special value that indicates that it is //pointing to nothing//. This special value has the name '' | + | You can set a pointer to a special value that indicates that it is //pointing to nothing//. This special value has the name '' |
- | If the memory location that a pointer variable will point to isn’t known at the time it is declared, then you should initialize it to '' | + | If the memory location that a pointer variable will point to isn’t known at the time it is declared, then you should initialize it to '' |
Below is an example of initializing a pointer variable to '' | Below is an example of initializing a pointer variable to '' | ||
<code cpp> | <code cpp> | ||
- | int *yourPtr = NULL; // yourPtr points to nothing | + | int *yourPtr = nullptr; // yourPtr points to nothing |
int y = 5; | int y = 5; | ||
yourPtr = & | yourPtr = & | ||
- | Note how '' | + | Note how '' |
- | '' | + | '' |
==== Pointer operators ==== | ==== Pointer operators ==== | ||
Line 161: | 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 176: | Line 177: | ||
cout << y; // prints 7</ | cout << y; // prints 7</ | ||
- | Think of the indirection/ | + | You can think of the indirection/ |
< | < | ||
Line 194: | 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 215: | Line 216: | ||
<< | << | ||
return 0; | return 0; | ||
- | }</ | + | } |
+ | </ | ||
cplusplus/pointers_1.1451595094.txt.gz · Last modified: 2015/12/31 20:51 by mithat