Pointers Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.

Slides:



Advertisements
Similar presentations
Computer Programming Lecture 14 C/C++ Pointers
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
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,
CSCI 171 Presentation 11 Pointers. Pointer Basics.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Computer Science 210 Computer Organization Pointers.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers CSE 2451 Rong Shi.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
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.
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:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
POINTERS.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
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.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
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.
UNIT 8 Pointers.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers What is the data type of pointer variables?
Computer Organization and Design Pointers, Arrays and Strings in C
Pointers.
Pointers and Pointer-Based Strings
Pointer.
Lecture 6 C++ Programming
Pointers Kingdom of Saudi Arabia
5.1 Introduction Pointers Powerful, but difficult to master
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
Creating and Using Pointer Variables in C++ By: Ed Brunjes
CISC181 Introduction to Computer Science Dr
Presentation transcript:

Pointers Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Pointers What are they for ? -Accessing array elements -Passing arguments to a function when the function needs to modify the original argument. -Passing arrays and strings to functions -Obtaining memory from the system -Creating data structures from linked lists

Pointers It is possible to do without pointers: arrays can be accessed with array notation rather than pointer notation a function can modify arguments passed, by reference, as well as those passed by pointers. However, in order to obtain the most from the language it is essential to use pointers.

The Address Operator & It is possible to find out the address occupied by a variable by using the address of the operator &. void main() { int var1 = 11; int var2 = 22; int var3 = 33; std::cout << std::endl << &var1 << std::endl << &var2 << std::endl << &var3; }

Addresses The actual addresses occupied by variables in a program depend on many factors, such as the computer the program is running on, the size of the operating system, and whether any other programs are currently in memory. For these reasons no two computers will give the same answer to the above program.

Creating Pointers For creation of a pointer variable that can hold the address of a data type int, an asterisk is used to write the type definition of ptr such as int*ptr; As a result of this declaration, room for an address is allocated to ptr.

The NULL Pointer No address has been placed in ptr, so its value is undefined. With ptr being undefined, a comparison involving p would be an error, although most C++ compilers would not flag the mistake. It is possible to assign the value constant NULL to indicate that ptr does not point to a memory allocation (ptr is no longer undefined).

Printing out variables that hold address values are useful. void main() {int var1 = 11; int* ptr; //defines a pointer std::cout <<std::endl <<&var1; ptr = &var1; std::cout <<std::endl <<ptr; } The * means pointer to. ptr is a pointer to an int (it can hold the address of integer variables).

Defining pointer variables defining pointer variables char*cptr; int*iptr; float*fptr; defining multiple pointer variables char*ptr1, * ptr2, * ptr3;

Putting values in pointers Before a pointer is used a specific address must be placed in it : ptr = &var1; A pointer can hold the address of any variable of the correct type; Warning! it must be given some value, otherwise it will point to an arbitrary address (because it has to point to something).

Accessing the variable pointed to void main() { int var1 = 22; int*ptr; ptr = &var1; std::cout <<std::endl <<*ptr; } ptr holds the address of var1 *ptr holds the contents of var1

Using Pointers to Modify Variables void main() { intvar1, var2; int* ptr; ptr = &var1; *ptr = 37; var2 = *ptr; std::cout <<std::endl<< var2; }

Indirect Addressing The * is used in declaration is different to the * used in assignment. Using the * to access the value stored in an addesss is called indirect addressing, or sometimes dereferencing the pointer.

Pointers/References Output //Pointers and references //pointersF.cpp #include int main() { int n = 10; int *pn = &n; int** ppn = &pn; std::cout<<"ppn="<<ppn<<std::endl; std::cout<<"*ppn="<<*ppn<<std::endl; std::cout<<"**ppn="<< **ppn<<std::endl; } ppn=0xa88b8 *ppn=0xa88bc **ppn=10

Pointers to Pointers? int n = 10; int *pn = &n; int** ppn = &pn; 100xa88bc0xa88b8 npnppn

In Summary... int v;//defines variable v of type int int*p;//defines p as a pointer to int p = &v;//assigns address of variable v //to pointer p v = 3;//assigns 3 to v *p = 3;//assigns 3 to v using indirect //addressing, referring to the same //variable (v) using its address.

Be Careful!!! Note : by declaring a pointer as void* ptr; is declaring a general purpose pointer that can point to any data type irrespective of its type. It is possible to assign the address of an int, float etc., to the same pointer variable, this eliminates the need to declare a separate pointer variable for each data type.

Void Pointers When used they must be type-cast! void increase (void* data) { switch (sizeof(data)) { case sizeof(char) : (*((char*)data))++; break; case sizeof(int) : (*((int*)data))++; break; } increase(‘x’); increase(9);