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 [2014/04/22 04:11] – [Example] 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. " | ||
+ | |||
===== Introduction ===== | ===== Introduction ===== | ||
Line 10: | 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 21: | Line 22: | ||
A regular variable occupies a fixed set of bytes in memory. For example, let's assume we want to create an '' | A regular variable occupies a fixed set of bytes in memory. For example, let's assume we want to create an '' | ||
- | ^ Variable name ^ Memory location ^ Memory contents | + | ^ Variable name ^ Memory location ^ Memory contents |
- | |'' | + | |'' |
|::: | |::: | ||
|::: | |::: | ||
Line 29: | 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 | + | ^ Variable name ^ Memory location ^ Memory contents |
- | |'' | + | |'' |
|::: | |::: | ||
|::: | |::: | ||
Line 41: | Line 42: | ||
|::: | |::: | ||
- | Note that while '' | + | Note that while '' |
==== Pointers in the abstract ==== | ==== Pointers in the abstract ==== | ||
Line 49: | 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 75: | 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 === | ||
- | To set the value of a pointer, you need the address of something. C++ has an address operator, ''&'', | + | To set the value of a pointer, you need the address of something. C++ has an **address operator**, ''&'', |
<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 92: | 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 123: | 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 156: | 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 171: | Line 177: | ||
cout << y; // prints 7</ | cout << y; // prints 7</ | ||
- | Think of the indirection/ | + | You can think of the indirection/ |
< | < | ||
Line 189: | 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 address of a is " << &a << endl | + | cout << "The value of a is: " |
- | << | + | << "The address of a is: " << &a << endl |
+ | << | ||
+ | |||
cout << endl; | cout << endl; | ||
- | + | ||
- | cout << "The value of a is " << a << endl | + | cout << "The value of a is: " << a << endl |
- | << | + | << |
cout << endl; | cout << endl; | ||
+ | |||
cout << " | cout << " | ||
<< | << | ||
Line 208: | Line 216: | ||
<< | << | ||
return 0; | return 0; | ||
- | }</ | + | } |
+ | </ | ||
cplusplus/pointers_1.1398139898.txt.gz · Last modified: 2014/04/22 04:11 by mithat