cplusplus:pointers_3
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
cplusplus:pointers_3 [2014/04/28 22:04] – created mithat | cplusplus: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 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. | + | </ |
+ | |||
+ | ===== 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: | ||
</ | </ | ||
- | * Pointers | + | And you can use array subscripting operations |
<code cpp> | <code cpp> | ||
Line 25: | Line 31: | ||
</ | </ | ||
- | * 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 | + | C++ lets you perform arithmetic on pointer variables; however, //pointer arithmetic works differently from normal arithmetic!/ |
- | * You may use the increment/decrement operator ('' | + | |
- | * You may add/ | + | |
- | * Pointers may be subtracted from each other. | + | |
- | * //Pointer arithmetic works differently from normal arithmetic!// | + | |
- | * Performed | + | |
- | * 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 < | #include < | ||
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--; |
// 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 | + | Subtracting |
- | * Returns | + | |
- | * Example: | + | |
- | <code cpp> | + | <file cpp subtracting-pointers.cpp> |
+ | /** Subtracting pointers */ | ||
#include < | #include < | ||
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 |
- | * Example: | + | |
- | <code cpp> | + | <file cpp walk-down-array.cpp> |
+ | /** Walking down an array. */ | ||
#include < | #include < | ||
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 < | #include < | ||
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 | ||
- | --------------------- | ||
- | '' | ||
- | '' | ||
- | '' | ||
- | '' | ||
- | |||
- | |||
- | ==== 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; | ||
</ | </ | ||
- | Operation | + | ==== Array access techniques ==== |
- | --------------------- | + | |
- | '' | + | ^ Access technique |
- | '' | + | | '' |
- | '' | + | | '' |
- | '' | + | | '' |
- | '' | + | | '' |
- | '' | + | |
+ | |||
+ | ==== Array arithmetic ==== | ||
+ | ^ Operation | ||
+ | |Increment < | ||
+ | |Decrement < | ||
+ | |Arithmetic < | ||
+ | |Compound assignment < | ||
+ | valptr += 2; // points at 11</ | ||
+ | |Pointer subtraction < | ||
cplusplus/pointers_3.txt · Last modified: 2016/03/05 23:27 by mithat