cplusplus:pointers_1_slides
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cplusplus:pointers_1_slides [2019/03/28 16:14] – mithat | cplusplus:pointers_1_slides [2021/10/19 19:29] (current) – [Example] mithat | ||
---|---|---|---|
Line 2: | Line 2: | ||
~~NOTOC~~ | ~~NOTOC~~ | ||
- | ====== Pointers 1 ====== | + | ====== Pointers 1 slides |
Pointer fundamentals, | Pointer fundamentals, | ||
Deitel, Harvey M., and Paul J. Deitel. " | Deitel, Harvey M., and Paul J. Deitel. " | ||
Mithat Konar\\ | Mithat Konar\\ | ||
- | March 28, 2019 | + | October 19, 2021 |
===== Introduction ===== | ===== Introduction ===== | ||
Line 23: | Line 23: | ||
===== Pointer hardware model ===== | ===== Pointer hardware model ===== | ||
- | Assume an '' | + | * Assume an '' |
^ Variable name ^ Memory location ^ Memory contents across all four bytes (32 bits)^ | ^ Variable name ^ Memory location ^ Memory contents across all four bytes (32 bits)^ | ||
Line 31: | Line 31: | ||
|::: | |::: | ||
- | The address of '' | + | * The address of '' |
===== Pointer hardware model ===== | ===== Pointer hardware model ===== | ||
- | A //pointer// variable set to point to '' | + | |
+ | * A //pointer// variable set to point to '' | ||
^ Variable name ^ Memory location ^ Memory contents across all 8 bytes (64 bits) ^ | ^ Variable name ^ Memory location ^ Memory contents across all 8 bytes (64 bits) ^ | ||
- | |'' | + | |'' |
|::: | |::: | ||
|::: | |::: | ||
|::: | |::: | ||
- | The //value// of the pointer variable is the base address | + | * The //value// of a pointer variable is the base address |
===== Pointer visual model ===== | ===== Pointer visual model ===== | ||
Line 60: | Line 61: | ||
===== Pointer syntax basics ===== | ===== Pointer syntax basics ===== | ||
- | ===== Declaration | + | ===== Pointer declaration |
* '' | * '' | ||
* Type of data being pointed to must also be indicated. | * Type of data being pointed to must also be indicated. | ||
Line 77: | Line 78: | ||
</ | </ | ||
- | ===== Declaration | + | ===== Pointer declaration |
* You can declare more than one pointer variable at a time: | * You can declare more than one pointer variable at a time: | ||
Line 99: | Line 100: | ||
* Most environments show base addresses as [[http:// | * Most environments show base addresses as [[http:// | ||
- | ===== Assignment | + | ===== Pointer assignment |
* Declare an integer variable '' | * Declare an integer variable '' | ||
Line 110: | Line 111: | ||
{{: | {{: | ||
- | ===== Assignment | + | ===== Pointer assignment |
* Declare an integer variable '' | * Declare an integer variable '' | ||
Line 129: | Line 130: | ||
- | ===== Assignment | + | ===== Pointer assignment |
- | * Chaning | + | * Changing |
<code cpp> | <code cpp> | ||
Line 142: | Line 143: | ||
</ | </ | ||
- | ===== Initialization | + | ===== Pinter initialization |
* Local pointer variables in C++ are not automatically initialized. | * Local pointer variables in C++ are not automatically initialized. | ||
* Uninitialized pointers point to arbitrary memory. //Very dangerous.// | * Uninitialized pointers point to arbitrary memory. //Very dangerous.// | ||
Line 154: | Line 155: | ||
===== nullptr/ | ===== nullptr/ | ||
- | * You can set a pointer to a value that indicates that it is //pointing to nothing//: '' | + | * You can set a pointer to a value that indicates that it is //pointing to nothing//: '' |
- | * Otherwise uninitialized pointers should be set to '' | + | * Otherwise uninitialized pointers should be set to '' |
<code cpp> | <code cpp> | ||
Line 164: | Line 165: | ||
* '' | * '' | ||
- | ===== Pointer operators | + | ===== Operators for pointers |
- | * **address operator** ''&'' | + | * **address operator** ''&'' |
- | * **indirection** or **dereferencing operator** '' | + | * **indirection** or **dereferencing operator** '' |
===== Indirection/ | ===== Indirection/ | ||
- | * **indirection** or **dereferencing** operator, '' | + | * **indirection** or **dereferencing** operator, '' |
<code cpp> | <code cpp> | ||
Line 181: | Line 182: | ||
===== Indirection/ | ===== Indirection/ | ||
- | * Dereferencing can be used to assign a value to a location in memory: | + | |
<code cpp> | <code cpp> | ||
Line 189: | Line 190: | ||
*myPtr = 7; // change value in y to 7 | *myPtr = 7; // change value in y to 7 | ||
cout << y; // prints 7</ | cout << y; // prints 7</ | ||
- | |||
- | ===== Indirection/ | ||
* You can think of the indirection/ | * You can think of the indirection/ | ||
Line 222: | Line 221: | ||
<< | << | ||
<< | << | ||
- | |||
cout << endl; | cout << endl; | ||
Line 233: | Line 231: | ||
<< | << | ||
<< | << | ||
+ | |||
return 0; | return 0; | ||
} | } | ||
</ | </ | ||
cplusplus/pointers_1_slides.txt · Last modified: 2021/10/19 19:29 by mithat