void SquareByAddress(int * ptr) // Note that this function doesn't return anything. void function. { *ptr = (*ptr) * (*ptr); // modifying the target, *ptr } int main() { int num = 4; cout << "num = " << num << '\n'; // num = 4 SquareByAddress(&num); // address of num passed in cout << "num = " << num << '\n'; // num = 16 }
int list[10]; // the variable list is a pointer // to the first integer in the array int * p; // p is a pointer. It has the same type as list. p = list; // legal assignment. Both pointers to ints.
In the above code, the address stored in list has been assigned to p. Now both pointers point to the first element of the array. Now, we could actually use p as the name of the array!
list[3] = 10; p[4] = 5; cout << list[6]; cout << p[6];
What pointer arithmetic operations are allowed?
Most often, pointer arithmetic is used in conjunction with arrays.
Example: Suppose ptr is a pointer to an integer, and ptr stores the address 1000. Then the expression (ptr + 5) does not give 1005 (1000+5). Instead, the pointer is moved 5 integers (ptr + (5 * size-of-an-int)). So, if we have 4-byte integers, (ptr+5) is 1020 (1000 + 5*4).
// This function receives two integer pointers, which can be names of integer arrays. int Example1(int * p, int * q);
When an array is passed into a function (by its name), any changes made to the array elements do affect the original array, since only the array address is copied (not the array elements themselves).
void Swap(int * list, int a, int b) { int temp = list[a]; list[a] = list[b]; list[b] = temp; }
This Swap function allows an array to be passed in by its name only. The pointer is copied but not the entire array. So, when we swap the array elements, the changes are done on the original array. Here is an example of the call from outside the function:
int numList[5] = {2, 4, 6, 8, 10}; Swap(numList, 1, 4); // swaps items 1 and 4
Note that the Swap function prototype could also be written like this:
void Swap(int list[], int a, int b);The array notation in the prototype does not change anything. An array passed into a function is always passed by address, since the array's name IS a variable that stores its address (i.e. a pointer).
Pass-by-address can be done in returns as well -- we can return the address of an array.
int * ChooseList(int * list1, int * list2) { if (list1[0] < list2[0]) return list1; else return list2; // returns a copy of the address of the array }
And an example usage of this function:
int numbers[5] = {1,2,3,4,5}; int numList[3] = {3,5,7}; int * p; p = ChooseList(numbers, numList);
const typeName * v
int Function1(const int * list); // the target of list can't // be changed in the function
Note: The pointer can be made constant, too. Here are the different combinations:
int * ptr;
const int * ptr;
int x = 5; int * const ptr = &x; // must be initialized hereAn array name is this type of pointer - a constant pointer (to non-constant data).
int x = 5; const int * const ptr = & x;
char name[20] = "Marvin Dipwart";Note that this declaration creates an array called name (of size 20), which can be modified.
char* greeting = "Hello";However, this does NOT create an array in memory that can be modified. Instead, this attaches a pointer to a fixed string, which is typically stored in a "read only" segment of memory (cannot be changed). So it's best to use const on this form of declaration:
const char* greeting = "Hello"; // better
name[1] = 'e'; // name is now "Mervin Dipwart" greeting[1] = 'u'; // ILLEGAL!