The document provides an outline and overview of pointers in C/C++. It discusses pointers as function arguments and return values. Key points include:
- Pointers can point to arrays, including multi-dimensional arrays.
- Pointers are passed by value to functions, but modifications through the pointer remain.
- Functions can return pointers to modify values in the caller or to return the address of a variable.
- Arrays are passed by reference, so modifications inside a function using the array argument remain in the caller.
- Pointer arithmetic and increment/decrement operators can modify values and pointers.
The document provides an outline and overview of pointers in C/C++. It discusses pointers as function arguments and return values. Key points include:
- Pointers can point to arrays, including multi-dimensional arrays.
- Pointers are passed by value to functions, but modifications through the pointer remain.
- Functions can return pointers to modify values in the caller or to return the address of a variable.
- Arrays are passed by reference, so modifications inside a function using the array argument remain in the caller.
- Pointer arithmetic and increment/decrement operators can modify values and pointers.
$%,14/+.( &+5 6/.1%+ 7&81/( 9&(/5 -+ (8*5/( :' #*&++& ;1 1 Outline Arrays in terms of pointers Multi-dimensional Pointer arrays Pointers as function arguments Pointers as function return value 2 Pointer Arrays: Pointer to Pointers Pointers can be stored in arrays Two-dimensional arrays are just arrays of pointers to arrays. int a[10][20]; int *b[10]; Declaration for b allows 10 int pointers, with no space allocated. Each of them can point to an array of 20 integers int c[20]; b[0] = c; What is the type of b? 3 "# $%%&'( *+. %-<( = >?@ *+. 2-8( = >?@
*+. :A%-<(BA2-8(B@
*+.CC & = +/< *+.CA%-<(B@ D-%E*+. * = ?@ * F %-<(@ GG*H &A*B = +/< *+.A2-8(B@ 4 Ragged Arrays 5 6&,,/5 "# $%%&'( *+. %-<( = >?@ *+.CC & = +/< *+.CA%-<(B@ D-%E*+. * = ?@ * F %-<(@ GG*H &A*B = +/< *+.A*B@ 6 Pointer Safety After you deallocate the memory associated with a pointer, set it to NULL for safety C++11 includes the nullptr keyword for a null pointer int* p = new int[50]; ... delete[] p; p = nullptr; While setting the pointer to 0 works, this isnt type-safe 7 Pointers are Passed by Value to Functions A copy of the pointers value is made the address stored in the pointer variable The copy is then a pointer pointing to the same object as the original parameter Thus modifications via de-referencing the copy STAYS. 8 Function Arguments x and y are copies of the original, and thus a and b can not be altered.
9 void swap(int x, int y) { int tmp; tmp = x; x = y; y = tmp; }
int main() { int a = 1, b = 2;
swap(a, b); return 0; } Wrong! Could use pass-by-reference, but lets use pointers! Recall: Pass by Reference // pass-by-reference (pass a reference to the argument) int f(int& a) { a = a+1; return a; }
int main() { int xx = 0; f(xx); // f() changed the value of xx cout << xx << endl; // writes 1 }
Really, you should only use pass-by-const-reference:
void g(int a, int& r, const int& cr) { ++a; ++r; int x = cr; ++x; }
int main() { int x = 0, y = 0, z = 0; g(x,y,z); // x==0; y==1; z==0 } // const references are very useful for passing large objects
10 0 xx: a: Pointers and Function Arguments Passing pointers a and b are passed by pointers (the pointers themselves px and py are still passed by value)
11 void swap(int *px, int *py) { int tmp; tmp = *px; *px = *py; *py = tmp; }
int main() { int a = 1, b = 2;
swap(&a, &b); return 0; } px a 1 py b 2 Use Pointers to Modify Multiple Values in a Function
12 void decompose(double d, int *i, double *frac) { *i = (int) d; *frac = d - *i; }
int main() { int int_part; double frac_part, input;
scanf("%lf", &input); decompose(input, &int_part, &frac_part); printf("%f decomposes to %d and %f\n", *int_part, *frac_part); return 0; } Pass by Reference Do not equate pass-by-reference with pass- by-pointer The pointer variables themselves are still passed by value The objects being pointed to, however, are passed by reference In a function, if a pointer argument is de- referenced, then the modification indirectly through the pointer will stay 13 Modification of a Pointer void g(int **ppx, int *py) { *ppx = py; }
int main() { int x = 1, y = 2, *px; px = &x; g(&px, &y); printf("%d", *px); // will print 2 } 14 Pointer as Return Value We can also write functions that return a pointer Thus, the function is returning the memory address of where the value is stored instead of the value itself Be very careful not to return an address to a temporary variable in a function!!! 15 Example of Returning a Pointer 16 int* max(int *x, int *y) { if (*x > *y) return x; return y; }
int main() { int a = 1, b = 2, *p;
p = max(&a, &b); return 0; } Example of Returning a Pointer How do these two code samples compare? Hint: x and y are copies of the original, so what are &x and &y?
17 int* max(int *x, int *y) { if (*x > *y) return x; return y; }
int main() { int a = 1, b = 2, *p;
p = max(&a, &b); return 0; } int* max(int x, int y) { if (x > y) return &x; return &y; }
p = max(a, b); Arrays as Arguments Arrays are passed by reference Modifications stay 18 #define SIZE 10
void init(int a[]) { int i;
for(i = 0;i<SIZE;i++){ a[i] = 0; } }
int main() { int a[SIZE];
init(a); return 0; } /* equivalent pointer alternative */ void init(int *a) { int i;
for(i = 0;i<SIZE;i++){ *(a+i) = 0; } } Pointer Arithmetic: Combining * and ++/-- ++ and -- has precedence over * a[i++] = j; p=a; *p++ = j; <==> *(p++) = j; *p++; value: *p, inc: p (*p)++; value: *p, inc: *p ++(*p); value: (*p)+1, inc: *p *++p; value: *(p+1), inc: p 19