© 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