CMPT 102 Introduction to Scientific Computer Programming Pointers © Janice Regan, CMPT 102, Sept. 2006 0 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. 2006 1 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. 2006 2 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. 2006 3 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. 2006 4 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. 2006 5 Declaring pointer variables Pointer variables can point to only one type of variable int *v1p; double *v2p; char *v3p; myStruct *v5p; © Janice Regan, CMPT 102, Sept. 2006 // pointer to an integer // pointer to a double // pointer to a char //pointer to a structure of // type myStruct 6 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. 2006 7 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. 2006 8 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 = -44.567; 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. 2006 9 Declared variables in memory Value of variable 76.2 address Identifier of variable v1 23.0 1000 1 1008 11.9 1016 v3 -23.7 1024 v4 -44.567 1032 v5 © Janice Regan, CMPT 102, Sept. 2006 v2 10 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. 2006 11 & 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. 2006 12 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 = -44.567; 4: v3p = &v3; 5: v5p = &v5; © Janice Regan, CMPT 102, Sept. 2006 13 After line 3 Pointer Identifier v3p v5p Value of pointer variable ? ? © Janice Regan, CMPT 102, Sept. 2006 Value of variable 76.2 address Identifier of variable v1 23.0 1000 1 1008 11.9 1016 v3 -23.7 1024 v4 -44.567 1032 v5 v2 14 After line 4 Pointer Identifier v3p v5p Value of pointer variable 1016 ? © Janice Regan, CMPT 102, Sept. 2006 Value of variable 76.2 address Identifier of variable v1 23.0 1000 1 1008 11.9 1016 v3 -23.7 1024 v4 -44.567 1032 v5 v2 15 After line 6 Pointer Identifier v3p v5p Value of pointer variable 1016 1032 © Janice Regan, CMPT 102, Sept. 2006 Value of variable 76.2 address Identifier of variable v1 23.0 1000 1 1008 11.9 1016 v3 -23.7 1024 v4 -44.567 1032 v5 v2 16 & 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. 2006 17 * 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. 2006 18 "Pointing to" Example Consider: v1 = 0; p1p = &v1; printf(“%d\n”, v1); *p1p = 42; printf(“%d, %d\n”, v1, *p1p); Produces output: 0 42 42 *p1p and v1 both refer to the value in the same location in memory © Janice Regan, CMPT 102, Sept. 2006 19 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. 2006 20 Pointer Assignments Graphic: Display 10.1 Uses of the Assignment Operator with Pointer Variables © Janice Regan, CMPT 102, Sept. 2006 21 Another example 1. int *v1p, *v2p, v1, v2; 2. v1p = &v1; 3. *v1p = 47; 4. v2p = v1p; 5. *v2p = 23; 6. v2p = &v2; 7. *v2p = 111; © Janice Regan, CMPT 102, Sept. 2006 22 After line 1 Pointer Identifier v1p v2p Value of pointer variable ? Value of variable ? ? address 1000 1 1004 Identifier of variable v1 v2 ? © Janice Regan, CMPT 102, Sept. 2006 23 After line 2 Pointer Identifier v1p v2p Value of pointer variable 1000 Value of variable ? ? address 1000 1 1004 Identifier of variable v1 v2 ? © Janice Regan, CMPT 102, Sept. 2006 24 After line 3 Pointer Identifier v1p v2p Value of pointer variable 1000 Value of variable 47 ? address 1000 1 1004 Identifier of variable v1 v2 ? © Janice Regan, CMPT 102, Sept. 2006 25 After line 4 Pointer Identifier v1p v2p Value of pointer variable 1000 Value of variable 47 ? address 1000 1 1004 Identifier of variable v1 v2 1000 © Janice Regan, CMPT 102, Sept. 2006 26 After line 5 Pointer Identifier v1p v2p Value of pointer variable 1000 Value of variable 23 ? address 1000 1 1004 Identifier of variable v1 v2 1000 NOTE: this also changes the value of *v1p © Janice Regan, CMPT 102, Sept. 2006 27 After line 6 Pointer Identifier v1p v2p Value of pointer variable 1000 Value of variable 23 ? address 1000 1 1004 Identifier of variable v1 v2 1000 © Janice Regan, CMPT 102, Sept. 2006 28 After line 7 Pointer Identifier v1p v2p Value of pointer variable 1000 Value of variable 23 111 address 1000 1 1004 Identifier of variable v1 v2 1000 © Janice Regan, CMPT 102, Sept. 2006 29 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 © Janice Regan, CMPT 102, Sept. 2006 30
© Copyright 2026 Paperzz