Programming fundamentals 2 Chapter 2:Pointer

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 CSE1301 Computer Programming Lecture 16 Pointers.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Review 1 List Data Structure List operations List Implementation Array Linked List.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
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.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Pointers What is the data type of pointer variables?
Pointers and Dynamic Arrays
Computer Skills2 for Scientific Colleges
CSE 220 – C Programming Pointers.
Pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers in C.
Learning Objectives Pointers Pointer in function call
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
INC 161 , CPE 100 Computer Programming
Introduction to Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointer.
Lecture 6 C++ Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Chapter 9: Pointers.
Pointers and Dynamic Variables
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
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
The Function Prototype
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
Chapter 9: Pointers and String
C++ Programming Lecture 18 Pointers – Part II
Pointers and dynamic objects
CISC181 Introduction to Computer Science Dr
Pointers.
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

Programming fundamentals 2 Chapter 2:Pointer Miss:Hanan Hardam

Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic

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

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

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

Cont. Example int *xPtr, *yPtr; int x = 5; xPtr = & x; yPtr = xPtr;

Cont. Example 4. cout << *xPtr; // this statement should print the value that is pointed to by xPtr, which is 5. 5. cout<< xPtr; // this statement should print the contents of xPtr location, which is the address 1008. - The same applies for yPtr, since it points to the same location as xPtr.

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 This will be discussed in more details in Arrays chapter.

Examples on Pointers //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; }

Call by Reference #include <iostream.h> void Increment(int&); void main() { int A = 10; Increment(A); cout<<A<<endl; } void Increment(int &X) { ++X; }

Call by Reference #include <iostream.h> void Increment(int&); //if you add extra cout statements to the program, the output will become: #include <iostream.h> void Increment(int&); void main() { int A = 10; cout << “in main “<< &A << endl; Increment(A); cout<<A<<endl; } void Increment(int &X) { ++X; cout << “in fun “<< X << endl; cout << “in fun “<< &X << endl;

Call by Pointer #include <iostream.h> void Increment(int*); void main() { int A = 10; Increment(&A); cout<<A<<endl; } void Increment(int *X) { ++*X; }

Call by Pointer //if you add extra cout statements to the program, the output will become: #include <iostream.h> void Increment(int*); void main() { int A = 10; cout << "in main "<< &A<< endl; Increment(&A); cout<<A<<endl; } void Increment(int *X) { ++*X; cout << "in fun X "<< X << endl; cout << "in fun *X "<< *X << endl; cout << "in fun &X"<< &X <<endl;

Examples on Pointers (Cont.) //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; // here the values which a and b point to are swapped *a = *b; *b = temp;