Data Structures and Algorithms Introduction to Pointers

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Pointers Applications
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
(continue) © by Pearson Education, Inc. All Rights Reserved.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Review 1 List Data Structure List operations List Implementation Array Linked List.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Pointers *, &, array similarities, functions, sizeof.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
EGR 2261 Unit 11 Pointers and Dynamic Variables
UNIT 5 C Pointers.
Pointers.
Chapter 7 - Pointers Outline 7.1 Introduction
Motivation and Overview
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers.
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
Student Book An Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
Passing Arguments to a Function
Programmazione I a.a. 2017/2018.
Pointer.
Lecture 6 C++ Programming
8 Pointers.
Chapter 7 from “C How to Program"
Pointers and References
Object Oriented Programming COP3330 / CGS5409
7 Arrays.
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Windows Programming Lecture 02.
Built-In (a.k.a. Native) Types in C++
Pointers and Pointer-Based Strings
Pointers.
Pointers Kingdom of Saudi Arabia
Outline Defining and using Pointers Operations on pointers
5.1 Introduction Pointers Powerful, but difficult to master
7 C Pointers.
Overloading functions
Pointers Pointers point to memory locations
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
OPERATORS in C Programming
Pointers and References
CISC181 Introduction to Computer Science Dr
Pointers, Dynamic Data, and Reference Types
OPERATORS in C Programming
Introduction to Pointers
Presentation transcript:

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

pointers Thing used To point at something

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

* & 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.

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 ;

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.

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 ;

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 ;

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 ;

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.

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.

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.

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.

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 ; }

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 ;

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.

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

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 ;

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 ;

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 ( 5 . 0 , 3 . 0 ) ; 7 p2 . p r i n t P o i n t ( ) ; 8 What is this pointer?