Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "© Janice Regan, CMPT 128, February. 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Pointers."— Presentation transcript:

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

2 © Janice Regan, CMPT 128, 2007-2013 1 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]

3 © Janice Regan, CMPT 128, 2007-2013 2 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

4 © Janice Regan, CMPT 128, 2007-2013 3 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

5 © Janice Regan, CMPT 128, 2007-2013 4 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.

6 © Janice Regan, CMPT 128, 2007-2013 5 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 +, -,++, --

7 © Janice Regan, CMPT 128, 2007-2013 6 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

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

9 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, 2007-2013 8

10 9 Pointers 76.2 23 11 a -44.567 1003 1005 1001 1002 1005 1003 1004 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

11 © Janice Regan, CMPT 128, 2007-2013 10 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.

12 © Janice Regan, CMPT 128, 2007-2013 11 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

13 © Janice Regan, CMPT 128, 2007-2013 12 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

14 © Janice Regan, CMPT 128, 2007-2013 13 & 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).

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

16 © Janice Regan, CMPT 128, 2007-2013 15 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;

17 © Janice Regan, CMPT 128, 2007-2013 16 * 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

18 © Janice Regan, CMPT 128, 2007-2013 17 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

19 © Janice Regan, CMPT 128, 2007-2013 18 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

20 © Janice Regan, CMPT 128, 2007-2013 19 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

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

22 © Janice Regan, CMPT 128, 2007-2013 21 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

23 © Janice Regan, CMPT 128, 2007-2013 22 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: 33 77 34

24 © Janice Regan, CMPT 128, 2007-2013 23 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

25 © Janice Regan, CMPT 128, 2007-2013 24 Pointers in assignment statements  Before executing statement  After executing statement firstp = secondp; 76.2 23 1003 1004 1003 1004 firstp secondp 76.2 23 1004 1003 1004 address Pointer Identifier firstp secondp Value of pointer variable address Pointer Identifier Value of pointer variable

26 © Janice Regan, CMPT 128, 2007-2013 25 Pointers in assignment statements  Before executing statement  After executing statement *firstp = *secondp; 76.2 23 1003 1004 1003 1004 firstp secondp 23 1003 1004 1003 1004 address Pointer Identifier firstp secondp Value of pointer variable address Pointer Identifier Value of pointer variable


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

Similar presentations


Ads by Google