BITG 1113: POINTER LECTURE 12 1
LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the concept of pointer. 2. Write declaration and initialization of a pointer. 3. Do arithmetic operation using pointer. 4. Describe the concept of pointer to pointer. 5. Use pointer as function parameters in program. 6. Describe the relationship between a pointer and an array. 7. Describe the concept of pointer to structure. 8. Use the selection operator 2
INTRODUCTION A pointer is a derived data type. Its value may be any of the addresses available in the computer for storing and accessing data. In a computer, every data or instruction of a process is stored in a compartment or small area inside the memory. These memories have their own addresses. By using this address, the computer can identify where a data or an instruction is stored physically in a computer. 3
Variable declaration : INTRODUCTION int idnumber = 5; From the declaration, we only know the value that is stored inside idnumber, without knowing where (the address) it is stored. The name of the variable is an alias @ (or a name) for the address where the value is stored. Each time, the variable is being used, the computer will translate the name of that variable to its address. By using pointer, the programmer could determine the address for any location where a value is stored. 4
POINTER DECLARATION Syntax : type * identifier; Example: 1. char g = Y ; char *p; p = &g; 2. double b = 2.5; double *q; q = &b; g is a variable which refers to a memory location that keeps a char value ( Y ). p is a pointer variable, that could keep the address of another memory location with a char value. Address (&) of variable g is assigned to p. In other words, p keeps the address of g. It means, p points to the location named as g that keeps a char value ( Y ). The type of pointer indicates the type of value the pointer points(refers)to. 5
POINTER DECLARATION WITH INITIALIZATION From Previous Example: 1. char g = Y ; char *p; p = &g; OR char *p = &g; 2. double b = 2.5; double *q; OR q = &b; double *q = &b; Pointer declaration with initialization 6
POINTER DECLARATION WITH INITIALIZATION In Step 2, suppose that the address of the int variable x is 9640. The statement in Step 2, assigns the address of x to p. We say that p points to x. x 7 p 9640 9640 9642 Since pointer p points to x, we can use p to get the integer value in x, by writing *p 7
EXAMPLE #include <iostream> int main() { int first = 8; int *paddress; paddress = &first; cout<< Content of first is << first <<endl; cout<< Content of *padress is << *paddress <<endl; cout<< Content of paddress is\n << paddress <<endl; cout<< Address of paddress is\n << &paddress <<endl; return 0; } first 8 paddress 4444 5555 first paddress 8 4444 4444 5555 Output: Content of first is 8 Content of *paddress is 8 Content of paddress is 4444 Address of paddress is 5555
#include <iostream> int main() { int first=7; int *paddress; paddress = &first; EXAMPLE first paddress 7 6666 8888 } cout<< Content of first is << first <<endl; cout<< Content of *paddress is << *paddress <<endl; *paddress = 52; cout<< Content of *paddress is << *paddress <<endl; cout<< Content of first is << first <<endl; return 0; first 7 paddress 6666 6666 8888 first 52 paddress 6666 6666 8888 Output: Content of first is 7 Content of *paddress is 7 Content of *paddress is 52 Content of first is 52
EXAMPLE The following program swaps the contents of the variables char1 and char2 but uses the address operator (&) and indirection operator (*) to do so. Line #include <iostream> int main() { 1 char char1 = A ; 2 char char2 = Z ; 3 char temp; 4 char *charptr; 5 charptr = &char1; 6 temp = *charptr; 7 *charptr = char2; 8 char2 = temp; 9 return 0; 10 } Trace this program. 10
line 1 char char1 = A ; 2 char char2 = Z ; 3 char temp; char1 char2 temp A Z 1293 7757 2131 11
line 4 char *charptr; char1 char2 temp charptr A Z 1293 7757 2131 4455 12
line 5 charptr = &char1; char1 char2 temp charptr A Z 1293 1293 7757 2131 4455 13
line 6 temp = *charptr; char1 char2 temp A Z A 1293 7757 2131 4455 charptr 1293 14
line 7 *charptr = char2; char1 char2 temp charptr Z Z A 1293 7757 2131 4455 1293 15
line 8 char2 = temp; char1 char2 temp Z A A 1293 7757 2131 4455 charptr 1293 16
EXAMPLE What is the output of this program? Line #include <iostream> int main() { 1 char c = O,d = H ; 2 char *p1, *p2, *temp; 3 p1 = &c; 4 p2 = &d; 5 temp = p1; 6 p1 = p2; 7 p2 = temp; 8 cout<< *p1 << *p2 ; 9 return 0; 10 } 17
line 1 char c = O, d = H ; 2 char *p1, *p2, *temp; 3 p1 = &c; 4 p2 = &d; c d p1 p2 temp O H 941 6940 941 6940 2131 4455 3456 18
line 5 temp = p1; c d p1 p2 temp O H 941 6940 941 941 6940 2131 4455 3456 19
line 6 p1 = p2; c d p1 p2 temp O H 6940 6940 941 941 6940 2131 4455 3456 20
line 7 p2 = temp; c d p1 O H 6940 941 6940 2131 p2 4455 941 temp 941 3456 line 8 cout<< *p1 << *p2 ; output : HO 21
ARITHMETIC OPERATION USING POINTER Arithmetic operation can also be done using pointer. A pointer (eg: ptr) will take the value stored at the address that is kept by the pointer (ie: *ptr) and compute them just like operations on normal variables such as add, multiply, divide, minus and unary operator. *ptr ptr? 558 22 558 1121
//Example: #include <iostream> int main() { } int number = 6, total; int *pnum = &number; total = *pnum * *pnum; cout<< Multiplication of *pnum with << *pnum is << total <<endl; return 0; number Output: Multiplication of *pnum with *pnum is 36 6 222 36 pnum 222 total 777 555 23
POINTER TO POINTER So far the pointers we have been using have pointed directly to data. It is possible and often with advanced data structures necessary to use pointers that point to other pointers. eg: *p **q dereferenced once dereferenced twice 24
EXAMPLE : #include <iostream> int main() { int a; // a is an integer variable int *p; // p is a pointer to an integer } a p q 58 4444 5555 4444 5555 6666 int **q; // q is a pointer to pointer to an integer a = 58; p = &a; q = &p; cout<< a <<endl; cout<< *p <<endl; cout<< **q <<endl; return 0; Output: 58 58 58 25
POINTERS AND FUNCTIONS C++ provides two ways to pass parameters to functions : pass by value and pass by reference. When we passed by reference, C++ passes the address of the parameter variable, and the parameter name becames an alias for the variable. We can add an alternative to pass by reference that is by using pass a pointer and use it to change the original variable. 26
EXAMPLE : PASS BY REFERENCE int main() { //Pass by reference int a = 5; int b = 7; exchange (a,b); return 0; a b } void exchange(int& x, int& y) { int temp = x; x = y; y = temp; return; } 5 7 7 5 x 5 temp y 27
int main() { //Passing pointers int a = 5; int b = 7; exchange (&a,&b); return 0; } EXAMPLE :PASSING POINTER void exchange(int* px, int* py) { int temp = *px; *px = *py; *py = temp; return; 5 7 7 5 } 28 a px 5 temp b py
RELATIONSHIP BETWEEN A POINTER AND AN ARRAY A pointer and an array have a close relationship. In C++ Programming Language, the name of an array (without the subscript) will refer to the address of the first element of that array. In other words, the name of an array is actually a pointer to that array. So, the name of an array can be used to be set as an initial value for a pointer. 29
The difference between a pointer and an array s name is, a pointer points to any address in the memory but the name of an array points only to the first element of that array and it is fixed. Example : RELATIONSHIP BETWEEN A POINTER AND AN ARRAY double value[10]; double *pvalue = value; From the declaration above, pvalue will store the address of the first element in array value, which is the address of value[0]. 30
To achieve the next element in an array, use this formula :- *( ptr + i ) where, i represents the location of the next element in the array, from where the pointer is at that moment. Example : RELATIONSHIP BETWEEN A POINTER AND AN ARRAY double value[5]={2.5, 4.1, 5.6, 4.7, 4.3}; double *pvalue = value;// pvalue points to 1 st element cout << *(pvalue + 2);// pvalue points to (1+2)element - The code will print the third element of the array : 5.6 31
Example of program that uses an array and index #include <iostream> int main() { const int size = 5; int i; int grade[size]={98,88,90,65,83}; } for( i = 0; i<size; i++) cout<< Element << i+1 << of array is << grade[i] << endl; return 0; Output: Element 1 of array is 98 Element 2 of array is 88 Element 3 of array is 90 Element 4 of array is 65 Element 5 of array is 83 Example of program that uses a pointer to refer the elements in an array. #include <iostream> int main() { const int size = 5; int i; int grade[size]={98,88,90,65,83}; int *pvalue = grade; //OR int *pvalue = &grade[0]; } for( i = 0; i<size; i++) cout<< Element << i+1 << of array is << *(pvalue+i) << endl; return 0;
RELATIONSHIP BETWEEN A POINTER AND AN ARRAY From the previous example, each element in array grade can be reach using pointer : Using Array Using Pointer Value grade[0] *pvalue 98 grade[1] *( pvalue + 1 ) 88 grade[2] *( pvalue + 2 ) 90 grade[3] *( pvalue + 3 ) 65 grade[4] *( pvalue + 4 ) 83 33
RELATIONSHIP BETWEEN A POINTER AND AN ARRAY Example : double value[5]={2.5,4.1,5.6,4.7,4.3}; double *pvalue = &value[1];//pvalue points to 2nd element cout << *(pvalue + 2);// pvalue points to (2+2)element - The code will print the fourth element of the array : 4.7 34
POINTER TO STRUCTURE Structure can be accessed through pointers. Example : use SAMPLE structure with pointers. The first thing is define a pointer for the structure as shown below. 35
POINTER TO STRUCTURE The parentheses (brackets) are absolutely necessary because the precedence priority of the member operator(.) is higher than the priority of the indirection operator (*). If the parentheses are not used, C++ applies the dot operator first and the asterisk operator next. In other words, *ptr.x is interpreted as *(ptr.x) 36
POINTER TO STRUCTURE 37
SELECTION OPERATOR Another operator that eliminates the problem with pointers to structures the selection operator. The selection operator (->) is at the same level in the Precedence Table as the member operator (.) The token for the selection operator is an arrow formed by the minus sign and the greater than symbol (->). The token is placed immediately after the pointer identifiers and before the member to be referenced. 38
INDIRECTION OPERATOR VS. SELECTION OPERATOR (* pointername). fieldname SAME AS pointername -> fieldname 39
REFERENCING MEMBER OF A STRUCTURE USING SELECTION OPERATOR 40 -END-