© Janice Regan, CMPT 128, February. 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Pointers.

Slides:



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

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Pointers CSE 2451 Rong Shi.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
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.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
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:
Primitive Variables.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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.
CPS120: Introduction to Computer Science Lecture 15A Structures.
Pointers *, &, array similarities, functions, sizeof.
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.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
© Janice Regan, CMPT 128, Feb CMPT 128: Introduction to Computing Science for Engineering Students Structures.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Engineering Computing I Chapter 5 Pointers and Arrays.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
© 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++
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?
Intro to Pointers in C CSSE 332 Operating Systems
The Machine Model Memory
Pointers Introduction
Learning Objectives Pointers Pointer in function call
Pointers and Pointer-Based Strings
FIGURE 9-5 Integer Constants and Variables
Chapter 9 Pointers Objectives
Lecture 6 C++ Programming
Pointers Psst… over there.
Andy Wang Object Oriented Programming in C++ COP 3330
Pointers Psst… over there.
Pointer Basics Psst… over there.
Topics discussed in this section:
Windows Programming Lecture 02.
Lecture 18 Arrays and Pointer Arithmetic
Pointers Lecture 1 Thu, Jan 15, 2004.
Java Programming Review 1
C++ Pointers and Strings
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
C Programming Pointers
Pointer Basics Psst… over there.
C++ Pointers and Strings
Presentation transcript:

© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers

© Janice Regan, CMPT 128, What is a variable?  A variable is stored in a particular location in memory  A variable is given an identifier (name) when it is declared. We refer to the variable using that identifier  A variable has a type  int myvariable1; //type int  float myarray[10]; //type float[10]

© Janice Regan, CMPT 128, Types of Constants and Variables  A Data Type is  A set of values  A set of operations that can be done on those values  A crucial concept on modern programming  The data type of a variable or constant determines  how the variable’s value will be represented in memory  how many bytes are used to represent the variable  Data types may represent  Numbers, addresses  Characters or strings  Other objects

© Janice Regan, CMPT 128, What is a pointer?  Each variable is stored at some memory address,  Count successive locations in memory  Assume the first memory location counted has address 0, the second address 1, and so on  Each location in memory has an address, that address can be represented as an integer  A pointer (or reference) is a special type of variable that holds a memory address  A pointer containing the address of a variable ‘points to‘ or ‘references’ that variable

© Janice Regan, CMPT 128, Types for pointers  Include a set of values  legal memory addresses  Include a set of operations on those values  +, -, --, ++ (meanings of operator somewhat different from simple arithmetic definitions)  Include a method to represent the values within the computer:  Addresses are represented like integers.  IMPORTANT: Addresses are not integers, they have different properties and applications than integers.

© Janice Regan, CMPT 128, Data Types, pointers and integers  Data type int includes  a set of objects, the integers (… -10, -9,-8, …,123, 124, …)  Operations that can be done on those objects (+, -, *, /, % …)  Data type ‘pointer to an integer’ includes  A set of objects, all legal addresses for integers  A set of operations +, -,++, --  Data type ‘pointer to an double’ includes  A set of objects, all legal addresses for doubles  A set of operations +, -,++, --

© Janice Regan, CMPT 128, 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, *xp; // 2 pointers to char, need * for each myStruct *v5p; //pointer to a structure of type myStruct

© Janice Regan, CMPT 128, 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;

Meaning of ++, --, +, -  There are several types of pointers  A pointer to an integer points at an integer  ++ means add the length of an int to the address  A pointer to a long long int  -- means subtract the length of a long long int from the address  For pointer x to a double  x = x+5 means add the 5 times the length of a double to the pointer x © Janice Regan, CMPT 128,

9 Pointers a address Value of variable Identifier of variable v1 v2 v5 v3 v4 Pointer Identifier v3p v5p Value of pointer variable Assuming length of each variable is 1 for simplicity

© Janice Regan, CMPT 128, Where do 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.

© Janice Regan, CMPT 128, Initializing pointers  After you declare a pointer int *v2p;  It is good programming practice to initialize the pointer to NULL v2p = NULL;  The value NULL is defined in

© Janice Regan, CMPT 128, Initializing pointers  int v1=12, *v1p;  v1p = NULL; ? 1004 v1p address Pointer Identifier Value of pointer variable 12 Variable Identifier Variable Value v1 NULL 1004 v1p address Pointer Identifier Value of pointer variable 12 Variable Identifier Variable Value v1

© Janice Regan, CMPT 128, & Operator (address of)  To find the address that a particular variable begins at you can use the unary operator & int v1, *v1p; v1p = &v1;  The operator & applied to the variable v1 gives the address of v1. The value of the expression &v1 is the address of v1.  In the case illustrated above &v1 is assigned to the “pointer to integer” variable v1p (using = operator).

© Janice Regan, CMPT 128, Pointers in assignment statements  int v1=12, *v1p;  v1p = &v1; ? 1004 v1p address Pointer Identifier Value of pointer variable 12 Variable Identifier Variable Value v v1p address Pointer Identifier Value of pointer variable 12 Variable Identifier Variable Value v1

© Janice Regan, CMPT 128, Initializing pointers  After you declare a pointer int *v1p, v1, *v2p, *v3p;  It is good programming practice to initialize the pointer to NULL or some other particular value. A common non NULL value is the address of some other already declared (non pointer) variable. v1p = &v1;

© Janice Regan, CMPT 128, * Operator: dereferencing  The dereferencing operator can only be applied to a pointer variable  *myp dereferences pointer myp,  To dereference means to extracts the value of the variable pointed to by the pointer myp  The expression *myp (other than in a declaration) has a value that is the value of the variable being pointed to double *myp=NULL, myv=29; myp = &myv; //make myp point at myv cout << *myp; //extract value of myv (pointed to by myp) and print it

© Janice Regan, CMPT 128, Dereferencing pointers  double *myp=NULL, myv=29;  myp = &myv; NULL 1004 myp address Pointer Identifier Value of pointer variable 29 Variable Identifier Variable Value myv 1004 v1p address Pointer Identifier Value of pointer variable 29 Variable Identifier Variable Value v1

© Janice Regan, CMPT 128, Dereferencing pointers  myp = &myv;  cout << *myp;  Looks for the address in myp  Goes to that address and interprets the value in the location beginning at that address as an integer  Prints the integer value to the screen 1004 myp address Pointer Identifier Value of pointer variable 29 Variable Identifier Variable Value myv

© Janice Regan, CMPT 128, Dereferencing example double f2, *f2p; f2p = &f2; f2 = 23; cout << *f2p << “ “ << f2:  There are two ways to refer to the value of variable f2  f2  *f2p  The result printed by the code is 23

© Janice Regan, CMPT 128, Dereferencing pointers  double f2, *f2p;  f2p = &f2; ? 1004 f2p address Pointer Identifier Value of pointer variable ? Variable Identifier Variable Value f f2p address Pointer Identifier Value of pointer variable ? Variable Identifier Variable Value f2

© Janice Regan, CMPT 128, Dereferencing pointers  f2 = 23;  cout << *f2p << “ “ << f2:  Looks for the address in f2p  Goes to that address  extracts the value beginning at that address  Interprets the extracted value as an integer  Prints the integer value to the screen  Prints the value of variable f2 to the screen 1004 f2p address Pointer Identifier Value of pointer variable 23 Variable Identifier Variable Value f2

© Janice Regan, CMPT 128, Another dereferencing Example  Consider: start and *startp refer to the same variable start = 33; startp = &start; cout << start << endl; *startp = 77; cout << start << “ “ << *startp << endl; start = 34; cout << start << “ “<< *startp<< endl;  Produces output:

© Janice Regan, CMPT 128, Pointers in assignment statements  One pointer can be assigned, using an assignment statement, to another pointer firstp = secondp;  After this statement firstp points to the same variable that secondp pointed to before the statement  The contents of the variable a pointer points to can be replaced with the contents of a variable pointed to by another pointer *firstp = *secondp  After this statement the contents of the variable pointed to by firstp is the same as the contents of the variable pointed to by secondp

© Janice Regan, CMPT 128, Pointers in assignment statements  Before executing statement  After executing statement firstp = secondp; firstp secondp address Pointer Identifier firstp secondp Value of pointer variable address Pointer Identifier Value of pointer variable

© Janice Regan, CMPT 128, Pointers in assignment statements  Before executing statement  After executing statement *firstp = *secondp; firstp secondp address Pointer Identifier firstp secondp Value of pointer variable address Pointer Identifier Value of pointer variable