Prepared by Andrew Jung
Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and initialization of the array */ int table [6] = {16, 21, 8, 3, -7, 9}; /* Declaration of a pointer to an integer. */ int *ptr /* The pointer, which is an address, is assigned the address of the first address of the first cell in the array. */ ptr = &table [0] Result of coding is illustrated in the following diagram. Array Name: Table /*Following printf function will display “16”*/ cout<<*ptr ptr = ptr+1 (or ptr++) causes the pointer to the second cell in the array ptrptr+1ptr+2ptr+3ptr+4ptr
Accessing Pointer Data When an array is declared, the name of the array is actually a pointer to the first element in the array. As an consequence array parameters are always passed to a function by reference. table[6] = {16, 21, 8, 3, -7, 9} ----> This is the same as following: *(table + 0) = 16 *(table + 1) = 21 *(table + 2) = 8 *(table + 3) = 3 *(table + 4) = -7 *(table + 5) = 9 But table[0] =16, table[1] =21, table[2]=8, table[3]=3, table[4]=-7 table[5]=9 Therefore, (table + i) is the same as the table[i]. where i = 0 to 5.
Accessing Pointer Data #include using namespace std; int main() { char a[4] ="HAL"; cout<<a[0]<<"\n"; //display 0th element of an array cout<<*a<<"\n"; //a is a character array. it prints 'H' which is first element of an array cout<<a; //a is a pointer to an array of characters cout<<"\n"; cout<<*(a+1)<<"\n"; cout<<*(a+2)<<"\n"; return 0; }
Accessing Pointer Data Example:
In-Class Activity 7 On Moodle…
Pointers as Function arguments and Parameters Two way to pass variables to a function by reference By using & smbol prior to the parameter in the function head By passing an array using an array name.
Pointers as Function arguments and Parameters #include using namespace std; void swap(int *p1, int *p2); int main() { int value1 = 10; int value2 = 20; int *pointer2 = &value2; //int *pointer1 = &value1; cout<<"The value before the function call: "<<value1<<" "<<value2<<endl; //swap(pointer1, pointer2); swap(&value1, pointer2); cout<<"The value after the function call: "<<value1<<“ "<<value2<<endl; return 0; } void swap(int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; }
Pointers as Function arguments and Parameters #include using namespace std; void Display_String(char *String); int main() { char *String = "HELLO"; Display_String(String); return 0; } void Display_String(char *String) { cout<<"The string value is: "<<String; }
Array of Pointers We can easily define an array that stores pointers, thus creating an array of pointers Example: char *ptr_Array[2]; ptr_Array[0] = “Dog”; ptr_Array[1] = “Cat”; First, define an array of pointers to character data. Since array size is two, this array holds two pointers, or addresses. Since an array name is always a pointer and this pointer points to the pointers contained in the array.
Array of Pointers ‘D’‘o’‘g’‘\0’ Address of “Dog” Ptr_Array[0] Ptr_Array *(Ptr_Array[0]) *(Ptr_Array[0]+1) Address of “Dog” Ptr_Array[1] ‘C’‘a’‘t’‘\0’ *Ptr_Array *(Ptr_Array[1]) *(Ptr_Array+1) *(Ptr_Array[1]+2)
In-Class-Activity 8 On Moodle…