User Tools

Site Tools


cplusplus:pointers_3_slides

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cplusplus:pointers_3_slides [2019/03/31 21:17] mithatcplusplus:pointers_3_slides [2021/10/24 02:46] (current) – [Pointer arithmetic: addition] mithat
Line 6: Line 6:
 Pointers, arrays, arithmetic.\\ Pointers, arrays, arithmetic.\\
 Mithat Konar\\ Mithat Konar\\
-March 312019+October 232021
  
 ===== Pointers and arrays ===== ===== Pointers and arrays =====
  
   * Arrays and pointers are closely related.   * Arrays and pointers are closely related.
-  * An array name is like a constant pointer:+  * Array names are like a constant pointers:
     * the block of memory where an array name points cannot be changed,     * the block of memory where an array name points cannot be changed,
     * what is stored there can change.     * what is stored there can change.
Line 51: Line 51:
 ===== Pointer arithmetic ===== ===== Pointer arithmetic =====
  
-C++ lets you perform arithmetic on pointer variables; however, //pointer arithmetic works differently from normal arithmetic!// You can add/subtract integers to/from a pointer using the corresponding operators (''+'',  ''+='', ''-'', ''-='', ''++'', ''--''), but when these operators are used with pointers, //the math is performed in terms of the size of the pointed data type.//+  * Pointer arithmetic works differently than normal arithmetic
 +  * ''+'',  ''+='', ''-'', ''-='', ''++'', ''--'' do math in terms of the size of the pointed data type. 
 +  * E.g., adding 1 to a pointer makes it point to the next block of memory corresponding to the size of the underlying type.
  
-In other words, adding 1 to a pointer makes it point to the next block of memory corresponding to the size of the underlying type; subtracting 1 from a pointer makes it point to the previous block of memory corresponding to the size of the underlying type. This is really only meaningful when performed on an array. 
  
 <file cpp pointer-math.cpp> <file cpp pointer-math.cpp>
Line 77: Line 78:
 } }
 </file> </file>
 +
 +===== Pointer arithmetic: pointer difference =====
  
 Subtracting one pointer from another returns the number of elements between two addresses: Subtracting one pointer from another returns the number of elements between two addresses:
Line 97: Line 100:
 </file> </file>
  
-==== Walking down an array ====+===== Walking down an array =====
  
 A common technique used to visit every element in an array is to //walk down an array// with a pointer: A common technique used to visit every element in an array is to //walk down an array// with a pointer:
Line 121: Line 124:
 </file> </file>
  
-This is especially common with zero-terminated (i.e., NULL terminated) partially-filled arrays:+Especially common with zero-terminated (i.e., NULL terminated) partially-filled arrays:
  
 <file cpp walk-null-terminated.cpp> <file cpp walk-null-terminated.cpp>
Line 145: Line 148:
 ===== Summary ===== ===== Summary =====
  
-The examples below assume:+The following examples below assume:
  
 <code cpp> <code cpp>
Line 152: Line 155:
 </code> </code>
  
-==== Array access techniques ====+===== Array access techniques =====
  
 ^ Access technique                               ^ Example                         ^ ^ Access technique                               ^ Example                         ^
Line 161: Line 164:
  
  
-==== Array arithmetic ==== +===== Array arithmetic =====
  
 ^ Operation                                                        ^ Example ^ ^ Operation                                                        ^ Example ^
-|Increment <code>pointer++</code>                                          |<code cpp>valptr++;  // points at 7</code>+|<code>pointer++</code>                                          |<code cpp>valptr++;  // points at 7</code>
-|Decrement <code>pointer--</code>                                          |<code cpp>valptr--;  // now points at 4</code>+|<code>pointer--</code>                                          |<code cpp>valptr--;  // now points at 4</code>
-|Arithmetic <code>pointer + int</code> and <code>pointer - int</code> |<code cpp>cout << *(valptr + 2); // prints 11</code>+|<code>pointer + int</code>                                      |<code cpp>cout << *(valptr + 2); // prints 11</code>
-|Compound assignment <code>pointer += int</code>and<code>pointer -= int</code> |<code cpp>valptr = vals; // points at 4+|<code>pointer += int</code>                                     |<code cpp>valptr = vals; // points at 4
 valptr += 2;   // points at 11</code> | valptr += 2;   // points at 11</code> |
-|Pointer subtraction <code>pointer - pointer</code>                                |<code cpp>cout << valptr - val; // # of ints between valptr and val</code> |+|<code>pointer - pointer</code>                                  |<code cpp>cout << valptr - vals; // # of ints between valptr and vals</code> |
  
cplusplus/pointers_3_slides.1554067027.txt.gz · Last modified: 2019/03/31 21:17 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki