BITG 1113: POINTER LECTURE 12

Similar documents
[0569] p 0318 garbage

BITG 1113: Function (Part 2) LECTURE 5

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

MYcsvtu Notes LECTURE 34. POINTERS

Pointers. 10/5/07 Pointers 1

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

! The address operator (&) returns the address of a. ! Pointer: a variable that stores the address of another

Lecture 23: Pointer Arithmetic

Pointers, Arrays and C-Strings

Pointers, Dynamic Data, and Reference Types

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

First of all, it is a variable, just like other variables you studied

Lecture 05 POINTERS 1

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Week 3: Pointers (Part 2)

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Reference operator (&)

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

Chapter 7 C Pointers

a data type is Types

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

C Pointers. 7.2 Pointer Variable Definitions and Initialization

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

CSC 211 Intermediate Programming. Arrays & Pointers

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Memory and Pointers written by Cathy Saxton

C++ Programming Chapter 7 Pointers

Variation of Pointers

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

pointers + memory double x; string a; int x; main overhead int y; main overhead

CSC 211 Intermediate Programming. Pointers

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Introduction to Computer Science Midterm 3 Fall, Points

Pointers and Strings. Adhi Harmoko S, M.Komp

What is an algorithm?

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Goals of this Lecture

Object oriented programming C++

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Language comparison. C has pointers. Java has references. C++ has pointers and references

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

School of Science and Technology

by Pearson Education, Inc. All Rights Reserved.

CPSC 427a: Object-Oriented Programming

Pointers. Memory. void foo() { }//return

CS201- Introduction to Programming Current Quizzes

Arrays and Pointers (part 1)

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Using Parallel Arrays. Parallel Array Example

Pointers. Reference operator (&) ted = &andy;

Arrays, Pointers and Memory Management

Homework #3 CS2255 Fall 2012

REFERENCES, POINTERS AND STRUCTS

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

CS31 Discussion 1E Spring 17 : week 08

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

BITG 1233: Introduction to C++

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 6: Pointers

12. Pointers Address-of operator (&)

What have we learned about when we learned about function parameters? 1-1

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Procedural programming with C

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CS 103 Unit 13 Slides

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

Module Operator Overloading and Type Conversion. Table of Contents

UNIT- 3 Introduction to C++

CA31-1K DIS. Pointers. TA: You Lu

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

CS2255 HOMEWORK #1 Fall 2012

C++ for Java Programmers

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings


Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

SYSC 2006 C Winter 2012

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

CA4003 Compiler Construction Assignment Language Definition

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

Pointers. Variable Declaration. Chapter 10

In this chapter, you will learn about: Pointers. Dynamic Arrays. Introduction Computer Science 1 CS 23021

Transcription:

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-