User Tools

Site Tools


cplusplus:pointers_3

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 [2014/04/28 23:46] mithatcplusplus:pointers_3 [2016/03/05 23:27] (current) – [Pointers 3] mithat
Line 1: Line 1:
 ====== Pointers 3 ====== ====== Pointers 3 ======
  
-Pointers, arrays, arithmetic.((Portions loosely adapted from: FIXME))+Pointers, arrays, arithmetic.
  
 ===== Pointers and arrays ===== ===== 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 array name points cannot be changed, but what is stored there can change.+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: The relationship is so close that you can use pointer operators with array names:
Line 39: Line 39:
 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.// 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.//
  
-In other words, adding 1 to a pointer makes it to point to the next block of memory corresponding to the size of the underlying type and subtracting 1 from a pointer makes to 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.+In other words, adding 1 to a pointer makes it point to the next block of memory corresponding to the size of the underlying typesubtracting 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>
 +/** Pointer arithmetic. */
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 65: Line 66:
 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:
  
-<file cpp pointer-subtraction.cpp>+<file cpp subtracting-pointers.cpp> 
 +/** Subtracting pointers */
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 83: Line 85:
 ==== Walking down an array ==== ==== Walking down an array ====
  
-A common technique used to visit every element in an array is to //walking 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:
  
 <file cpp walk-down-array.cpp> <file cpp walk-down-array.cpp>
 +/** Walking down an array. */
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 94: Line 97:
     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 107: Line 110:
  
 <file cpp walk-null-terminated.cpp> <file cpp walk-null-terminated.cpp>
 +/** Walking down a null-terminated array. */
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 146: Line 150:
  
 ^ Operation                                                        ^ Example ^ ^ Operation                                                        ^ Example ^
-|<code>{pointer}++</code>                                          |<code cpp>valptr++;  // points at 7</code>+|Increment <code>pointer++</code>                                          |<code cpp>valptr++;  // points at 7</code>
-|<code>{pointer}--</code>                                          |<code cpp>valptr--;  // now points at 4</code>+|Decrement <code>pointer--</code>                                          |<code cpp>valptr--;  // now points at 4</code>
-|<code>{pointer{int}</code>and<code>{pointer{int}</code>   |<code cpp>cout << *(valptr + 2); // prints 11</code>+|Arithmetic <code>pointer + int</code> and <code>pointer - int</code> |<code cpp>cout << *(valptr + 2); // prints 11</code>
-|<code>{pointer+= {int}</code>and<code>{pointer-= {int}</code> |<code cpp>valptr = vals; // points at 4+|Compound assignment <code>pointer += int</code>and<code>pointer -= int</code> |<code cpp>valptr = vals; // points at 4
 valptr += 2;   // points at 11</code> | valptr += 2;   // points at 11</code> |
-|<code>{pointer{pointer}</code>                                |<code cpp>cout << valptr - val; // number of ints between valptr and val</code> |+|Pointer subtraction <code>pointer - pointer</code>                                |<code cpp>cout << valptr - val; // of ints between valptr and val</code> |
  
cplusplus/pointers_3.1398728774.txt.gz · Last modified: 2014/04/28 23:46 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki