Programming fundamentals 2 Chapter 3:Pointer

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
Thursday, January 11, 2007 Harrisberger's Fourth Law of the Lab: Experience is directly proportional to the amount of equipment ruined Harrisberger's Fourth.
1 CSE1301 Computer Programming Lecture 16 Pointers.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
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)
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
 2007 Pearson Education, Inc. All rights reserved. 1 C Pointers Chapter 7 from “C How to Program" Another ref:
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
1 Lecture 12 Pointers and Strings Section 5.4, ,
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 7.7Pointer Expressions and Pointer Arithmetic Arithmetic operations can be performed on pointers –Increment/decrement pointer ( ++ or -- ) –Add an integer.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Lecture 9 - Pointers 1. Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Computer Skills2 for Scientific Colleges
Week 9 - Pointers.
Chapter 7 - Pointers Outline 7.1 Introduction
POINTERS.
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
INC 161 , CPE 100 Computer Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
8 Pointers.
CSI-121 Structured Programming Language Lecture 16 Pointers
Chapter 7 from “C How to Program"
Programming fundamentals 2 Chapter 2:Pointer
Pointers and Dynamic Variables
Pointers and Pointer-Based Strings
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Pointers Lecture 2 Tue, Jan 24, 2006.
5.1 Introduction Pointers Powerful, but difficult to master
7 C Pointers.
The Function Prototype
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
POINTERS.
Chapter 9: Pointers and String
C++ Programming Lecture 18 Pointers – Part II
CISC181 Introduction to Computer Science Dr
Presentation transcript:

Programming fundamentals 2 Chapter 3:Pointer Miss:Hanan Hardam

Computer Skills2 for Scientific Colleges Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic Relations Between Pointers and Arrays Pointers and Strings Computer Skills2 for Scientific Colleges

Computer Skills2 for Scientific Colleges Overview of Pointers A Pointer in C++ is variable whose value is a memory address. With pointers many memory locations can be referenced. Some data structures use pointers (e.g. linked list, tree). The * and & operators - & operator is the address operator - * operator is the dereferencing operator. It is used in pointers declaration Computer Skills2 for Scientific Colleges

Computer Skills2 for Scientific Colleges Pointer Declaration Pointers are declared as follows: <type> * variable_name ; e.g. int * xPtr; // xPtr is a pointer to data of type integer char * cPtr; //cPtr is a pointer to data of type character void * yPtr; // yPtr is a generic pointer, // represents any type Computer Skills2 for Scientific Colleges

Computer Skills2 for Scientific Colleges Pointer Assignment Assignment can be applied on pointers of the same type If not the same type, a cast operator must be used Exception: pointer to void does not need casting to convert a pointer to void type void pointers cannot be dereferenced Example int *xPtr, *yPtr; int x = 5; … xPtr = & x; // xPtr now points to address of x yPtr = xPtr; // now yPtr and xPtr point to x Computer Skills2 for Scientific Colleges

Computer Skills2 for Scientific Colleges Pointer Arithmetic Increment / decrement pointers ( ++ or -- ) Add / subtract an integer to/from a pointer ( + or += , - or -= ) Pointers may be subtracted from each other Pointer arithmetic is meaningless unless performed on an array Computer Skills2 for Scientific Colleges

Pointer Arithmetic (Cont.) Example Consider an integer array of 5 elements on a machine using 4 bytes for integers. 1000 1004 1008 1012 1016 Pointer variable vPtr vPtr pointes to first element V[0] (location 1000) i.e. vPtr = 1000 vPtr +=2; sets vPtr to 1008 i.e. vPtr points to V[2] V[0] V[1] V[2] V[3] V[4] Computer Skills2 for Scientific Colleges

Pointer Arithmetic (Cont.) Subtracting pointers - Returns the number of elements between two addresses e.g. if v is an array and vPtr1 = v[0]; vPtr2 = v[2]; then vPtr2 – vPtr1 = 2 (i.e. 2 addresses) Computer Skills2 for Scientific Colleges

Relations Between Pointers and Arrays Arrays and pointers are closely related. - Array name is like constant pointer - Pointers can do array subscribing operations - If we declare an array A[4] and a pointer aPtr  aPtr is equal to A aPtr == A  aPtr is equal to the address of the first element of A aPtr == & A[0] Computer Skills2 for Scientific Colleges

Relations Between Pointers and Arrays (Cont.) Accessing array elements with pointers: - Element A[i] can be accessed by *(aPtr+i)  This is called pointer/offset notation - Array itself can use pointer arithmetic  A[3] is same as *(A+3) - Pointers can be subscripted (i.e. pointer/subscript notation)  aPtr [3] is same as A[3] Computer Skills2 for Scientific Colleges

Computer Skills2 for Scientific Colleges Examples on Pointers //File: swap.cpp //A program to call a function to swap two numbers using reference parameters  #include <iostream.h> void swap(int *, int *); // This is swap's prototype void main() { int x = 5, y = 7; swap(&x , &y); // calling swap with reference parameters cout << "\n x is now "<< x << " and y is now " << y << '\n'; } // swap function is defined here using dereferencing operator ‘*’ void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; Computer Skills2 for Scientific Colleges

Examples on Pointers (Cont.) //File: pointers.cpp //A program to test pointers and references #include <iostream.h> void main ( ) { int intVar = 10; int *intPtr; // intPtr is a pointer intPtr = & intVar; cout << "\nLocation of intVar: " << & intVar; cout << "\nContents of intVar: " << intVar; cout << "\nLocation of intPtr: " << & intPtr; cout << "\nContents of intPtr: " << intPtr; cout << "\nThe value that intPtr points to: " << * intPtr; } Computer Skills2 for Scientific Colleges

Computer Skills2 for Scientific Colleges Pointers and Strings Pointers can be used to declare strings and with string functions (see next lecture slides) Computer Skills2 for Scientific Colleges

Computer Skills2 for Scientific Colleges When calling, the pointer formal parameter will points to the actual parameter. #include <iostream.h> void Increment(int*); void main() { int A = 10; Increment(&A); cout<<A<<endl; } void Increment(int *X) { ++*X; } Computer Skills2 for Scientific Colleges