We think you have liked this presentation. If you wish to download it, please recommend it to your friends in any social system. Share buttons are a little bit lower. Thank you!
Presentation is loading. Please wait.
Published byJaiden Langworthy
Modified about 1 year ago
© 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; //type float
© 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
Computer Programming Lecture 14 C/C++ Pointers. Pointer Variable Declarations and Initialization Why we need Pointers:Why we need Pointers: –Pointes enable.
Pointers. BY SIVAKUMAR.P Pointers OBJECTIVES INTRODUCTION POINTERS DYNAMIC MEMORY ALLOCATION LINKED LISTS APPLICATION.
1 CSci 1130 Intro to Programming in Java Variables and Calculations.
Variables in C Amir Haider Lecturer. Topics Naming Variables Naming Variables Declaring Variables Declaring Variables Using Variables Using Variables.
An introduction to pointers in c Pointers are Great!!! Pointers: are extremely powerful make some things much easier improve your program's efficiency.
Copyright © 2003 Pearson Education, Inc. Slide 1.
First Program in JAVA: Printing a Line of Text Modifying our First Java Program Escape Sequences Displaying Text with printf Data types in JAVA.
Constants, Variables, and Data Types. Like any other language, C has its own vocabulary and grammar. In this chapter, we will discuss the concepts of.
Chapter 17 vector and Free Store Bjarne Stroustrup
Data Variable and Pointer Variable Pass by Reference Pointer Arithmetic Passing Array Using Pointers Dynamic Allocation.
Principles of Programming Chapter 7: Arrays In this chapter, you will learn about Introduction to Array Array declaration Array initialization Assigning.
MT311 (Oct 2007) Java Application Development Variables – binding, type, and scope Expression Tutorial 6.
2.1 Program Construction In Java Joey Militti Brendan Tackney Period 1.
Computer Architecture and the Fetch-Execute Cycle Von Neumann Architecture.
CS61CL L01 Introduction (1) Huddleston, Summer 2009 © UCB Introduction to C Jeremy Huddleston inst.eecs.berkeley.edu/~cs61c.
Chapter 9: Strings Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman 1-1.
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.
Chapter 21 Implementing lists: array implementation.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object.
Copyright © 2009 Elsevier Chapter 3:: Names, Scopes, and Bindings Programming Language Pragmatics Michael L. Scott.
Object Oriented Intro. Objectives Why Object-Oriented? Real-World Object/Programmed Objects? What we want in objects? Object Relationships?
25-Aug-14 Pointers and References. Machine addresses Computer memory consists of one long list of addressable bytes A pointer is a data item that contains.
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Of. and a to the in is you that it at be.
BD05/06 PL/SQL Introduction Structure of a block Variables and types Accessing the database Control flow Cursors Exceptions Procedures.
Sub and Function Procedures. Objectives Explain the difference between a Sub procedure and a Function procedure Create a Sub procedure Create a procedure.
© 2016 SlidePlayer.com Inc. All rights reserved.