260 likes | 416 Views
pointer. What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers. pointer. A pointer is a variable that contains a memory address as its value. count. countPtr. 10. pointer. count. countPtr. 1000. 10.
E N D
pointer • What it is • How to declare it • How to use it • Relationship between arrays and pointers • Relationship between strings and pointers
pointer A pointer is a variable that contains a memory address as its value count countPtr 10
pointer count countPtr 1000 10 memory address1000 count 10 countPtr 1000
How to declare a pointer int * countPtr; countPtr is a variable of type “pointer to int”
example int count = 10; int *countPtr = 1000; memory address 1000 count 10 countPtr 1000
How to initialize a pointer to point to an address int count = 10; int * countPtr = &count; & is an operator that returns the address of a variable. memory address 1000 count 10 countPtr 1000
Restrictions for using & operator • May be used for variables int count; int *countPtr = &count; • May not be used with constants (&10)
How to initialize a pointer to point to nothing int * aPtr = NULL; OR int * aPtr; aPtr = NULL;
Indirect and direct references Direct reference – changing count memory location using count variable Indirect reference – changing count memory location using countPtr variable countPtr count 10
Indirect and direct references int count; int * countPtr = &count; count = 12; // direct reference *countPtr = 12 // indirect reference * is the indirection operator. Means change the memory location currently pointed to by countPtr. countPtr count 12 10
Exercises • Declare an integer variable called x. Declare a pointer to x called xPtr and initialize it so that it points to the variable x. Use xPtr to indirectly change the value of x to 20; • Declare a second integer variable called y. Declare a pointer to y called yPtr and initialize it so that it points to the variable y. Calculate the sum of x + y using xPtr and yPtr.
pointer arithmetic int *aPtr; aPtr++; // is valid but what does it do? integer (4 bytes) aPtr aPtr is incremented by the size of whatever it is pointing to. In this example 4 bytes. So the value in aPtr is increased by 4 by the aPtr ++ statement
pointer arithmetic char *aPtr; aPtr++; char (1 byte) aPtr aPtr is incremented by the size of whatever it is pointing to. In this example 1 byte. So the value in aPtr is increased by 1 by the aPtr ++ statement
using pointer arithmetic int gradesPtr = &grades[0]; gradePtr 100 grades[0] grades[1] grades[2] grades[3] grades[4] 85 50 gradesPtr++; gradePtr 65 88
using pointer arithmetic char letterPtr = &letters[0]; letterPtr ‘a’ letters[0] letters[1] letters[2] letters[3] letters[4] ‘b’ ‘c’ letterPtr ++; letterPtr ‘d’ ‘e’
pointers and arrays • array name is a pointer. This is why: • can pass array name to a function without the brackets • arrays are passed to functions as reference parameters by default
arrays as function parameters Pass the name of the array without the brackets void PrintResults(int results[ ]); // function prototype void PrintResults(int results[ ]) // function definition { } calling sequence: const int maxRange = 10; int frequency[maxRange ] = {0}; PrintResults (frequency);
pointers and arrays int b[ ] = {1,2,3,4,5}; b 1 b[0] b[1] b[2] b[3] b[4] 2 3 4 5
pointers and arrays b int *bPtr; int b[ ] = {1,2,3,4,5}; bPtr = &b[0]; or bPtr = b; bPtr 1 b[0] b[1] b[2] b[3] b[4] 2 3 4 5
pointers and strings char digits [ ] = {“0123456789”}; digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] ‘0’ ‘1’ ‘2’ ‘3’ ‘4’ ‘5’ ‘6’ ‘7’ ‘8’ ‘9’ NULL
pointers and strings char digits [ ] = {“0123456789”}; digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] ‘0’ char *digitsPtr = digits; ‘1’ ‘2’ digitsPtr ‘3’ ‘4’ ‘5’ ‘6’ ‘7’ ‘8’ ‘9’ NULL
simplify Instead of char digits [ ] = {“0123456789”}; char *digitsPtr = digits; do char *digitsPtr = “0123456789”;
simplify char *digitsPtr = “0123456789”; cout << digitsPtr << endl; // displays all the numbers in the string
Simplify Array of strings const int Size = 5; const int WordSize = 10; char article [Size] [WordSize] = {"the", "a", "one", "some", "any"}; char noun [Size] [WordSize] = {"boy", "girl", "dog", "town", "car"}; char verb [Size] [WordSize] = {"drove", "jumped", "ran", "walked", "skipped"}; char preposition [Size] [WordSize] = {"to", "from", "over", "under", "on"};
Array of strings const int Size = 5; char *article [Size] = {"the", "a", "one", "some", "any"}; char *noun [Size] = {"boy", "girl", "dog", "town", "car"}; char *verb [Size] = {"drove", "jumped", "ran", "walked", "skipped"}; char *preposition [Size] = {"to", "from", "over", "under", "on"};