User Tools

Site Tools


cplusplus:pointers_3

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
cplusplus:pointers_3 [2014/04/28 22:04] – created mithatcplusplus:pointers_3 [2016/03/05 23:21] – [Array arithmetic] mithat
Line 1: Line 1:
 ====== Pointers 3 ====== ====== Pointers 3 ======
  
-===== The relationship between pointers and arrays =====+Pointers, arrays, arithmetic.((Portions loosely adapted from: FIXME))
  
-  * Arrays and pointers in C++ closely related. +<WRAP center round important 60%> 
-  * In C++ an //array name// is like a //constant pointer//. +This content has not been vetted for C++11 compliance. 
-    * The block of memory where //array name// points cannot be changed, but what is stored there can change.+</WRAP> 
 + 
 +===== Pointers and arrays ===== 
 + 
 +Arrays and pointers in C++ are very closely related. In C++ an array name is like a constant pointer: the block of memory where an array name points cannot be changed, but what is stored there can change. 
 + 
 +The relationship is so close that you can use pointer operators with array names:
  
 <code cpp> <code cpp>
Line 15: Line 21:
 </code> </code>
  
-  * Pointers can perform array subscripting operations.+And you can use array subscripting operations with pointers:
  
 <code cpp> <code cpp>
Line 25: Line 31:
 </code> </code>
  
-  * The following are equivalent+The following are equivalent:
  
 <code cpp> <code cpp>
Line 35: Line 41:
 ===== Pointer arithmetic ===== ===== Pointer arithmetic =====
  
-  * C++ lets you perform arithmetic on pointer type variables+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.//
-    * You may use the increment/decrement operator (''++'' or ''--''). +
-    * You may add/subtract an integer to/from a pointer ( ''+'' or ''+='' , ''-'' or ''-='')+
-    * Pointers may be subtracted from each other. +
-  * //Pointer arithmetic works differently from normal arithmetic!// +
-    * Performed in terms of the size of the pointed data type. +
-    * Really only meaningful when performed on an array. +
-  * Example:+
  
-<code cpp>+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> 
 +/** Pointer arithmetic. */
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 57: Line 59:
                             // i.e., v[3]                             // i.e., v[3]
     cout << *vPtr << endl;  // prints v[3]     cout << *vPtr << endl;  // prints v[3]
-    vPtr--;                 // vPtr now points to address sizeof(int) bytes old value,+    vPtr--;                 // vPtr now points to address vPtr - 1*sizeof(int),
                             // i.e., v[2]                             // i.e., v[2]
     cout << *vPtr << endl;  // prints v[2]     cout << *vPtr << endl;  // prints v[2]
Line 64: Line 66:
     return 0;     return 0;
 } }
-</code>+</file>
  
-  * Subtracting pointers +Subtracting one pointer from another returns the number of elements between two addresses:
-    * Returns the number of elements between two addresses+
-    * Example:+
  
-<code cpp>+<file cpp subtracting-pointers.cpp> 
 +/** Subtracting pointers */
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 84: Line 85:
     return 0;     return 0;
 } }
-</code>+</file>
  
 ==== Walking down an array ==== ==== Walking down an array ====
  
-  * Pointer notation is often used to //walk down an array//+A common technique used to visit every element in an array is to //walk down an array// with a pointer:
-  * Example:+
  
-<code cpp>+<file cpp walk-down-array.cpp> 
 +/** Walking down an array. */
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 100: Line 101:
     int *vPtr = v;     int *vPtr = v;
  
-    for(int i=0; i<5; i++)+    for (int i = 0; i < 5; i++)
     {     {
         cout << *vPtr << endl;         cout << *vPtr << endl;
Line 108: Line 109:
     return 0;     return 0;
 } }
-</code>+</file>
  
-  * Example assuming 0 is a sentinel:+This is especially common with zero-terminated (i.e., NULL terminated) partially-filled arrays:
  
-<code cpp>+<file cpp walk-null-terminated.cpp> 
 +/** Walking down a null-terminated array. */
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 118: Line 120:
 int main() int main()
 { {
-    int v[] = {2, 4, 6, 8, 12, 0};+    int v[20] = {2, 4, 6, 8, 12};
     int *vPtr = v;     int *vPtr = v;
  
Line 129: Line 131:
     return 0;     return 0;
 } }
-</code>+</file>
  
 ===== Summary ===== ===== Summary =====
- 
-==== Array access ==== 
- 
-Array access method                             Example 
----------------------                           --------------- 
-''array-name[N]''                                 ''  vals[2] = 17;'' 
-''pointer-to-array[N]''                           ''  valptr[2] = 17;'' 
-''array-name'' and subscript arithmetic           ''  *(vals + 2) = 17;'' 
-''pointer-to-array'' and  subscript arithmetic    ''  *(valptr + 2) = 17;'' 
- 
- 
-==== Array arithmetic ====  
  
 The examples below assume: The examples below assume:
  
 <code cpp> <code cpp>
-int vals[] = {4,7,11};+int vals[] = {4, 7, 11};
 int *valptr = vals; int *valptr = vals;
 </code> </code>
  
-Operation                                       Example +==== Array access techniques ==== 
----------------------                           --------------- + 
-''{pointer}++''                                   ''valptr++ // points at 7'' +^ Access technique                               Example                         ^ 
-''{pointer}--''                                   ''valptr-- // now points at 4'' +''arrayName[N]''                               |<code cpp>vals[2] = 17;</code>       | 
-''{pointer}+{int}'', ''{pointer}-{int}''            ''cout << *(valptr + 2); // 11'' +''pointerName[N]''                             |<code cpp>valptr[2] = 17;</code>     | 
-''{pointer}+={int}'', ''{pointer}-={int}''          ''valptr = vals; // points at 4'' +''arrayName'' and subscript arithmetic         |<code cpp>*(vals + 2) = 17;</code>   | 
-                                                ''valptr +2;   // points at 11'' +''pointerName'' and  subscript arithmetic      |<code cpp>*(valptr + 2) 17;</code> | 
-''{pointer}-{pointer}''                           ''cout << valptr-val; // difference (number of ints) between valptr and val''+ 
 + 
 +==== Array arithmetic ==== 
  
 +^ Operation                                                        ^ Example ^
 +|Increment <code>pointer++</code>                                          |<code cpp>valptr++;  // points at 7</code> |
 +|Decrement <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> |
 +|Compound assignment <code>pointer += int</code>and<code>pointer -= int</code> |<code cpp>valptr = vals; // points at 4
 +valptr += 2;   // points at 11</code> |
 +|Pointer subtraction <code>pointer - pointer</code>                                |<code cpp>cout << valptr - val; // # of ints between valptr and val</code> |
  
cplusplus/pointers_3.txt · Last modified: 2016/03/05 23:27 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki