1 / 31

CMPT 102 Introduction to Scientific Computer Programming

CMPT 102 Introduction to Scientific Computer Programming. Pointers. Pointer Introduction. Pointer A special type of variable that holds the memory address of another variable Think about the memory in your computer

len-vincent
Télécharger la présentation

CMPT 102 Introduction to Scientific Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMPT 102Introduction to Scientific Computer Programming Pointers

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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. 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.!

  10. 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

  11. Declared variables in memory Value of variable Identifier of variable address 76.2 10001 v1 23.0 1008 v2 11.9 1016 v3 -23.7 1024 v4 -44.567 1032 v5

  12. 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;

  13. & 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"

  14. 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;

  15. After line 3 Value of variable Identifier of variable Value of pointer variable Pointer Identifier address 76.2 10001 v1 ? v3p 23.0 1008 v2 11.9 1016 v3 v5p ? -23.7 1024 v4 -44.567 1032 v5

  16. After line 4 Value of variable Identifier of variable Value of pointer variable Pointer Identifier address 76.2 10001 v1 1016 v3p 23.0 1008 v2 11.9 1016 v3 v5p ? -23.7 1024 v4 -44.567 1032 v5

  17. After line 6 Value of variable Identifier of variable Value of pointer variable Pointer Identifier address 76.2 10001 v1 1016 v3p 23.0 1008 v2 11.9 1016 v3 v5p 1032 -23.7 1024 v4 -44.567 1032 v5

  18. & 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

  19. * 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

  20. "Pointing to" Example • Consider: v1 = 0; p1p = &v1;printf(“%d\n”, v1); *p1p = 42; printf(“%d, %d\n”, v1, *p1p); • Produces output:042 42 • *p1p and v1 both refer to the value in the same location in memory

  21. 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 "valuepointed to" by p2

  22. Pointer Assignments Graphic: Display 10.1 Uses of the Assignment Operator with Pointer Variables

  23. Another example • int *v1p, *v2p, v1, v2; • v1p = &v1; • *v1p = 47; • v2p = v1p; • *v2p = 23; • v2p = &v2; • *v2p = 111;

  24. After line 1 Value of variable Identifier of variable Value of pointer variable Pointer Identifier address ? 10001 v1 ? v1p ? 1004 v2 v2p ?

  25. After line 2 Value of variable Identifier of variable Value of pointer variable Pointer Identifier address ? 10001 v1 1000 v1p ? 1004 v2 v2p ?

  26. After line 3 Value of variable Identifier of variable Value of pointer variable Pointer Identifier address 47 10001 v1 1000 v1p ? 1004 v2 v2p ?

  27. After line 4 Value of variable Identifier of variable Value of pointer variable Pointer Identifier address 47 10001 v1 1000 v1p ? 1004 v2 v2p 1000

  28. After line 5 Value of variable Identifier of variable Value of pointer variable Pointer Identifier address 23 10001 v1 1000 v1p ? 1004 v2 v2p 1000 NOTE: this also changes the value of *v1p

  29. After line 6 Value of variable Identifier of variable Value of pointer variable Pointer Identifier address 23 10001 v1 1000 v1p ? 1004 v2 v2p 1000

  30. After line 7 Value of variable Identifier of variable Value of pointer variable Pointer Identifier address 23 10001 v1 1000 v1p 111 1004 v2 v2p 1000

  31. Define Pointer Types • Can "name" pointer types • To be able to declare pointers like othervariables • 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

More Related