Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms Introduction to Pointers

Similar presentations


Presentation on theme: "Data Structures and Algorithms Introduction to Pointers"— Presentation transcript:

1 Data Structures and Algorithms Introduction to Pointers
Dr. Muhammad Safyan Department of computer Science Government College University, Lahore

2 pointers Thing used To point at something

3 Pointer is a variable (pointer variable) which holds the memory addresses as their value.
Usually a variable contains some specific value, we say variable directly references a value a pointer variable contains the address of a variable which hold some specific value, we say pointer indirectly references a value, called indirection. 1021 1017 1013 1009 1005 1001 . 105 1017 variable 1009 Points to the location 1017 pointer variable Table: memory layout

4 * & is the unary address operator.
It produces the address of its right-hand operand. * is the unary indirection (or dereferencing) operator. It takes an operand not as a value to use, but as the address of the value to use. This operator can be stacked (e.g., ****cptr) i.e used more than once in same line.

5 dataType *identifier;
Pointers, like any other variables, must be declared before they can be used. The declaration , dataType *identifier; i n t ∗ count ; We read it from right to left, count is pointer to integer. Yes count is now a pointer to an integer type variable/memory location. Similarly, 1 char ∗ c Ptr ; \\c Ptr i s a p o i n t e r to a c h a r a c t e r v a l u e 2 f l o a t ∗ f P t r ; \\f P t r i s a p o i n t e r to f l o a t v l a u e 3 i n t ∗ i Pt r , count ; \\ What i s t h i s ? 4 i n t ∗ i Pt r , \\j P t r ; 5 i n t ∗ i P t r = NULL ; \\ NULL p o i n t e r Address of operator (&) is used to initialize a pointer variable. Address of operator (&) returns the address of already existing variable. i P t r = & count ;

6 int* p, q; int *p, *q; p is a pointer variable, q is an int variable.
Above statement can also be written as int *p, q; int *p, *q; declares both p and q to be pointer variables of type int. Because the value of a pointer is a memory address, a pointer can store the address of a memory space of the designated type. For example, if p is a pointer of type int, p can store the address of any memory space of type int. C++ provides two operators—the address of operator (&) and the dereferencing operator ( *)—to work with pointers. The next two sections describe these operators.

7 The * operator commonly referred to as the dereferencing operator, returns value for the variable to which its operand points. For example 1 i n t count = 5 ; 2 i n t ∗ i P t r = & count ; 3 cout << ∗ i P t r <<e n d l ; 4 cout << count <<e n d l ; 5 ∗ i P t r = 1 0 ; 6 cout << count <<e n d l ; 7 c in >> ∗ i P t r ;

8 Pointer declaration (int *) Reference operator (&)
Dereferencing operator(*) Address of operator(&) 1 i n t count = 5 ; 2 i n t & temp = count ; 3 cout << temp <<e n d l ; 4 cout << count <<e n d l ; 5 temp = 1 0 ; 6 cout << count <<e n d l ; 7 cout << &count <<e n d l ; 8 cout << &temp<<e n d l ; 9 i n t ∗ i P t r = & count ; 10 cout << ∗ i P t r <<e n d l ;

9 Pointer pointing a pointer
pointer to a pointer to some object. i n t count = 1 0 ; 2 i n t ∗p = & count ; 3 i n t ∗∗q = &p ; 1021 . 1017 45 Variable 1013 1009 pointer variable 1005 1001 pointer to pointer cout << count << ∗p << ∗∗q <<e n d l ; cout << p << q <<e n d l ; cout << &p << ∗q << &q <<e n d l ;

10 The const qualifier enables the programmer to inform the compiler that value of a particular variable should not be modified. The const qualifier can be used to enforce the principle of least privilege. Always award a function enough access to the data in its parameters to accomplish its specified task, but no more. For example, size of array is passed in a function as parameter, its value should not be changed in a function accidentally so it should be treated as const.

11 There are several possible combinations of const with pointers.
int * ptr ptr is a pointer to integer. Both the data pointed at and where the pointer points can be modified. const int * ptr This is read: ptr is a pointer to a constant integer. This means that what it points at cannot be changed (the integer is constant), but the pointer itself can be changed (it can be set to point at something else, though it still can’t be used for changing values). Using this helps avoid accidentally changing data you are using int * const ptr This is read: ptr is a constant pointer to an integer. This means that we can change what the pointer is pointing at (e.g., *ptr = something), but cannot make the pointer point somewhere else (the pointer is constant) const int * const ptr This is read: ptr is a constant pointer to a constant integer. We can neither change the data pointed at nor where the pointer is pointing.

12 Pointers can be incremented (++) and decremented (--)
Integers can be added or subtracted from pointers (+, +=, —, -=) One pointer may be subtracted from another. Pointers are valid operands in arithmetic expressions, assignment expression and comparison expression. The general rule to remember is that the arithmetic is always done in terms of the size of the data type pointed at. For example, ptr += 2 means “change what we are pointing at by 2 * sizeof the data type”; if the datatype is 8 bytes, then ptr now points 2 * 8 = 16 bytes higher. ptr2 — ptr1 gives you the difference between the two pointers as a difference between the array index value of ptr2 and ptr1, so if ptr2 points to array[3] and ptr1 points to array[1], ptr2 — ptr1 = 2 (since the ptr2 points to an element two elements away from ptr1). Pointer math only makes sense in the context of an array.

13 There are three ways to pass arguments in C++: • Call by value:
The value of the operand is placed on the stack and the called function gets a copy of the value, not the original. The called function doesn’t even know where the original variable is, so there is no way to modify it. • Call by reference: To specify that a parameter is passed by reference, place an ampersand (‘&’) in front of the variable name in the prototype and function declaration. The compiler takes care of all the work so that when the variable is used in the local function, the original variable gets updated. • Call by address: The value passed is the address of the data structure we need to deal with, not the value of the data structure. Consequently, all references to that data structure will require the dereferencing operator *. Note: An astute observer may point out that a call by address is actually a call by value in which the value is a memory address. This is true. We make the distinction here only for sake of explanation.

14 char *func_1(int request, struct *sptr, int &orig)
i n t cube By Value ( i n t ) ; v o i d c u b e By Re f e r e n c e ( i n t &) ; v o i d c u b e By R e f e r e n c e Po i n t e r ( i n t ∗) ; i n t main ( v o i d ){ i n t number = 2 ; cout<<”The o r i g i n a l v a l u e o f number i s ”<< number<<e n d l ; i n t cube = cube By Value ( number ) ; cout<< ”The v l a u e o f number a f t e r c a l l B y V a l u e () i s ”<<number <<e n d l ; c u b e By Re f e r e n c e ( number ) ; cout<< ”The v a l u e o f number a f t e r c a l l B y R e f e r e n c e ( ) i s ” << number<<e n d l ; c u b e By R e f e r e n c e Po i n t e r (&number ) ; cout<<”The v a l u e o f number a f t e r c a l l B y R e f e r e n c e P o i n t e r i s ”<< number<<e n d l ; r e t u r n 0 ; }

15 Array name is a constant pointer to first element.
1017 56 a[4] or *(a+4) 1013 89 a[3] or *(a+3) 1009 a[2] or *(a+2) 1005 12 a[1] or *(a+1) 1001 34 a[0] or *(a+0) . 0100 A Table: 1D-Array cout << &a [ 0 ] <<e n d l ; cout << a <<e n d l ; cout << a [ 0 ] <<e nd l ; cout << ∗a<<e n d l ;

16 A pointer can be used to access an array.
i n t a [ 5 ] = { 2 0 , 3 0 , 4 0 , 5 0 , 6 0 } ; i n t ∗p = a ; cout << p [ 2 ] <<e n d l ; p = &a [ 2 ] ; cout << p [ −2 ] <<e n d l ; Some conclusions address calculations *(a+index): we know the pointer arithmetic. What about *a+ index ? *(a+index) is equivalent to a[index]: [] is fancy dereferencing operator array name is constant pointer it can’t be allocated to point to some other location.

17 What is the output of **twod
1 i n t twod [ 3 ] [ 4 ] = {{ 0 , 1 , 2 , 3 } , { 4 , 5 , 6 , 7 } , { 8 , 9 , 1 0 , 1 1 } } ; 2 3 f o r ( i n t i =0; i <3; i ++){ 4 f o r ( i n t j =0; j <4 ; j ++) 5 cout << twod [ i ] [ j ] << ”=” << ∗( ∗( twod+ i )+j ) << 6 e n d l ; } 7 } 8 What is the output of **twod

18 c o n s t char ∗ s u i t [ 4 ] = {” He a r t s ” , ” Diamond” , ” Clubs ” , ” Spades ”}
cout << s u i t [ 0 ] <<e n d l ;

19 Let we have a class Point
2 3 4 p r i v a t e : f l o a t x , y ; p u b l i c : 5 Point ( ) { 6 x= 0 ; y = 0 ; 7 } A pointer to an object can be declared as 1 i n t main ( ) { Point p1 , p2 ( 3 , 4 ) ; Point ∗ p3 = &p1 ;

20 How to access class members using a pointer to object of the class?
p1 . p r i n t P o i n t ( ) ; p2 . p r i n t P o i n t ( ) ; 1 2 ( ∗ p3 ) . p r i n t P o i n t ( ) ; 3 p3−>s e t P o i n t ( 2 , 3 ) ; 4 p1 . p r i n t P o i n t ( ) ; p3 = &p2 ; 5 6 p3−>s e t P o i n t ( , ) ; 7 p2 . p r i n t P o i n t ( ) ; 8 What is this pointer?


Download ppt "Data Structures and Algorithms Introduction to Pointers"

Similar presentations


Ads by Google