© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Pointers.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Functions Call by reference.
Introduction to Computers and Programming Lecture 7:
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
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.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Pointers CSE 2451 Rong Shi.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
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.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
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 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
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.
Characters and Strings
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Introduction to C Programming CE Lecture 6 Functions, Parameters and Arguments.
Operator Overloading Introduction
CSE 220 – C Programming Expressions.
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
CSE 220 – C Programming Pointers.
UNIT 5 C Pointers.
Pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
Learning Objectives Pointers Pointer in function call
Student Book An Introduction
COMP 2710 Software Construction Pointers
INC 161 , CPE 100 Computer Programming
Chapter 9 Pointers Objectives
Lecture 6 C++ Programming
Computer Science 210 Computer Organization
Pointer Basics Psst… over there.
Introduction to Abstract Data Types
Lecture 18 Arrays and Pointer Arithmetic
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Data Structures and Algorithms Introduction to Pointers
C Programming Pointers
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointer Basics Psst… over there.
Introduction to Pointers
Presentation transcript:

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers

© Janice Regan, CMPT 102, Sept Pointer Introduction  Pointer  A special type of variable that holds the memory address of another variable  Think about the memory in your computer  Conceptually, consider memory as a sequence of bytes (eight bit chunks, that can hold 8 zeros or ones)  All bytes are numbered, the first byte would be byte 0, the second byte 1 and so on  Variables of different types can have different sizes (use different numbers of bytes of memory)  The address of the variable is byte number in memory where the stored variable starts  You’ve seen pointers already  Call-by-reference parameters: Address of actual argument was passed

© Janice Regan, CMPT 102, Sept What is a pointer?  Each variable is stored at some memory address,  Each location in memory has an address, that address can be represented as an integer  A pointer is a special type of variable that holds a memory address  A pointer is printed with a %p  A printed memory address looks like 0x22ccdc  The integer includes characters because it is shown in hexadecimal (base 16)  A pointer containing the address of a variable 'points to' that variable

© Janice Regan, CMPT 102, Sept Addresses and Numbers  Pointers have their own types, these types  Hold addresses (represented by integers)  Point to variables of a particular type There is a different pointer type to point to each type of variable  Even though an address is represented by an integer it is not of type integer  Therefore print with special conversion %p

© Janice Regan, CMPT 102, Sept Data Types, pointers and integers  int is a data type  a set of objects, the integers … -10, -9,-8, …,123, 124, …  A set of operations that can be done on those objects +, -, *, /, % …  A pointer to an integer is a data type  A set of objects, integers that represent the addresses of each location in the computers memory  A set of operations that can be done on those objects +, -,++, -- … Does not make sense to %, * or / an address, %,*, / are not valid operations on addresses

© Janice Regan, CMPT 102, Sept Declaring Pointer Variables  Pointers declared like other types  Add "*" before variable name  Produces "pointer to" that type  "*" must precede each variable that is to have a pointer type  int *v1p, *v2p, v1, v2;  v1p, v2p hold pointers to int variables  v1, v2 are ordinary int variables  Note that each pointer variable (reference) must have the * as the first character, the * is associated with the variable identifier not the type

© Janice Regan, CMPT 102, Sept Declaring pointer variables  Pointer variables can point to only one type of variable int *v1p; // pointer to an integer double *v2p; // pointer to a double char *v3p; // pointer to a char myStruct *v5p; //pointer to a structure of // type myStruct

© Janice Regan, CMPT 102, Sept Pointers and Style  It is useful to easily be able to see which variables in your function are pointers  C does not enforce any particular structure on your variable and pointer identifiers  To distinguish pointers from other variables a number of conventions are used in different coding standards  In this course the coding standard suggested is to assure that all pointer identifiers end in p to indicate they are pointers.  double myVariable, *myVariablep;

© Janice Regan, CMPT 102, Sept Pointer Variables  Similarity of pointer variables and other types of variables  Can store value of the variable in a memory location For pointer variables the address of the variable pointed to is contained in the associated memory location For non pointer types the value of the variable is contained in the associated memory location  Not int, double, etc. Instead: A POINTER to int, double, etc.!

© Janice Regan, CMPT 102, Sept Pointer Variables  Example: 1: double *v3p, *v5p, v1, v2, v3, v4, v5; 2: v1 = 76.2; v2 = 23.0; v3 = 11.9; 3: v4 = -23.7; v5 = ;  v3p, v5p are declared as "pointer to double" variables  v3p can hold pointers to variables of type double Cannot hold pointers to other types!  v1, v2, v3, v4, v5 are double variables each double takes 8 bytes

© Janice Regan, CMPT 102, Sept Declared variables in memory address Value of variable Identifier of variable v1 v2 v5 v3 v4

© Janice Regan, CMPT 102, Sept Where do declared pointers point  When you declare a pointer int *v1p, v1;  Pointer variable v1p points to a random location in memory.  Why? v1p has not been initialized, it contains whatever was in the memory location associated with v1p before it was associated with v1p  This can cause serious problems, accessing and changing values that are not even associated with your program.  It is good programming practice to initialize all pointer to NULL. This means initialize the pointer to point nowhere. v1p = NULL;

© Janice Regan, CMPT 102, Sept & Operator  & is the unary "address of" operator  &myVariable  Used to determine the address of myVariable  The value of the expression &myVariable is an address  Example int *v3p, *v4p, v1, v2; v1 = 23; v3p = &v1;  Sets pointer variable v3p to "point to" int variable v1  Read like:  "v3p is assigned the address of v1"  Or "v3p points to v1"

© Janice Regan, CMPT 102, Sept Pointer Variables  Example: 1: double *v3p, *v5p, v1, v2, v3, v4, v5; 2: v1 = 76.2; v2 = 23.0; v3 = 11.9; 3: v4 = -23.7; v5 = ; 4: v3p = &v3; 5: v5p = &v5;

© Janice Regan, CMPT 102, Sept After line ? ? address Value of variable Identifier of variable v1 v2 v5 v3 v4 Pointer Identifier v3p v5p Value of pointer variable

© Janice Regan, CMPT 102, Sept After line ? address Value of variable Identifier of variable v1 v2 v5 v3 v4 Pointer Identifier v3p v5p Value of pointer variable

© Janice Regan, CMPT 102, Sept After line address Value of variable Identifier of variable v1 v2 v5 v3 v4 Pointer Identifier v3p v5p Value of pointer variable

© Janice Regan, CMPT 102, Sept & Operator  & is the unary "address of" operator  Also used to specify call-by-reference parameter  For example the function declared below has one parameter. v1. that is called by reference. int funSample(int *v1);  In the code calling this function variables v1 and v2 are defined int v1; int v2, int solution; The function is called twice solution = funSample(&v1) + funSample(&v2);  The arguments of the function calls (&v1, &v2) are references to or “pointers to” the variables used as arguments

© Janice Regan, CMPT 102, Sept * Operator: dereferencing  * The dereferencing operator  This operator can only be applied to a pointer variable  Consider the pointer variable f2p that points to variable f2 (f2 = &f2p)  f2p hold the address of the memory location in which f2 is stored  There are two ways to refer to the value of variable f2 in an expression  f2  *f2p

© Janice Regan, CMPT 102, Sept "Pointing to" Example  Consider: v1 = 0; p1p = &v1; printf(“%d\n”, v1); *p1p = 42; printf(“%d, %d\n”, v1, *p1p);  Produces output:  *p1p and v1 both refer to the value in the same location in memory

© Janice Regan, CMPT 102, Sept Pointer Assignments  Pointer variables can be "assigned": int *p1, *p2; p2 = p1;  Assigns one pointer to another  "Make p2 point to where p1 points"  Do not confuse with: *p1 = *p2;  Assigns "value pointed to" by p1, to "value pointed to" by p2

© Janice Regan, CMPT 102, Sept Pointer Assignments Graphic: Display 10.1 Uses of the Assignment Operator with Pointer Variables

© Janice Regan, CMPT 102, Sept Another example  int *v1p, *v2p, v1, v2;  v1p = &v1;  *v1p = 47;  v2p = v1p;  *v2p = 23;  v2p = &v2;  *v2p = 111;

© Janice Regan, CMPT 102, Sept After line 1 ? ? ? ? address Value of variable Identifier of variable v1 v2 Pointer Identifier v1p v2p Value of pointer variable

© Janice Regan, CMPT 102, Sept After line 2 ? ? 1000 ? address Value of variable Identifier of variable v1 v2 Pointer Identifier v1p v2p Value of pointer variable

© Janice Regan, CMPT 102, Sept After line 3 47 ? 1000 ? address Value of variable Identifier of variable v1 v2 Pointer Identifier v1p v2p Value of pointer variable

© Janice Regan, CMPT 102, Sept After line 4 47 ? address Value of variable Identifier of variable v1 v2 Pointer Identifier v1p v2p Value of pointer variable

© Janice Regan, CMPT 102, Sept After line 5 23 ? address Value of variable Identifier of variable v1 v2 Pointer Identifier v1p v2p Value of pointer variable NOTE: this also changes the value of *v1p

© Janice Regan, CMPT 102, Sept After line 6 23 ? address Value of variable Identifier of variable v1 v2 Pointer Identifier v1p v2p Value of pointer variable

© Janice Regan, CMPT 102, Sept After line address Value of variable Identifier of variable v1 v2 Pointer Identifier v1p v2p Value of pointer variable

© Janice Regan, CMPT 102, Sept Define Pointer Types  Can "name" pointer types  To be able to declare pointers like other variables  Eliminate need for "*" in pointer declaration  typedef int* IntPtr;  Defines a "new type" alias  Consider these declarations: IntPtr p; int *p; The two are equivalent