1 / 67

Pointers

Pointers. Why Pointers. They provide the means by which functions can modify arguments in the calling function. They support dynamic memory allocation . They provide support for dynamic data structures, such as binary trees and linked lists. What Are Pointers.

donaldw
Download Presentation

Pointers

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

  2. Why Pointers • They provide the means by which functions can modify arguments in the calling function. • They support dynamic memory allocation. • They provide support for dynamic data structures, such as binary trees and linked lists.

  3. What Are Pointers • A pointer is the memory address of an object. • A pointer variable is a variable that is specifically declared to hold a pointer to an object of its specified type. • This address is the location of anotherobject (typically another variable) in memory.

  4. Memory address Variable in memory 1000 1001 1002 1003 1004 1005 1003 . . .

  5. Pointer Declaration • General syntax: type *name; int *m; //The variable m can hold a //pointer to type int. char *ch; int count, *y, q;

  6. Two Pointer Operators Address Operator: & Dereference Operator: * Both & and * have a higher precedence than all other arithmetic operators except the unary minus, with which they share equal precedence.

  7. & Operator • The & is a unary operator that returns the memory address of its operand. & “the address of” m = &count; m receives the address of count. m count 100

  8. * Operator * is the complement of &. It is a unary operator that returns the value located at the address that follows. * “at address” q = *m; q receives the value at address m. ?

  9. #include <stdio.h> int main(void){  int target, source=10;  int *m;  m = &source;  target = *m;   printf("%d", target);   return 0;} Put the value 10 into a variable called target.

  10. Pointer Assignments • You can use a pointer variable on the right-hand side of an assignment statement to assign its value to another pointer. • When both pointers are the same type, the situation is straightforward.

  11. #include <stdio.h>int main(void){  int x = 99;  int *p1, *p2;  #include <stdio.h>int main(void){  int x = 99;  int *p1, *p2;   p1 = &x;  p2 = p1;    printf("%p %p", p1, p2);   printf(''%d %d\n", *p1, *p2);     return 0;}

  12. Illustrates Distinction between a pointer var value & aDereferenced var. main( ) { int i = 777, *p = &i; printf (“value of i:%d\n”, *p); printf (“Addr of i:%u or %p\n”, p, p); } Output Value of i: 777 Address of i: 234880259 or dfffcfc u - (unsigned Dec integer) p - (whatever way is Default for system) - Here is Hex.

  13. Example 1 int i = 1, *j, *k; Assume addresses of i, j, k are respectively Byte addresses 10, 20, 30 i:10 j:20 k:30 • j = &i; int var Pointer var Pointer var i:10 j:20 k:30 1 ? ? 1 10 ?

  14. 1 2 10 ? 2. *j = 2; i:10 j:20 k:30 Stores 2 at the memory location pointed to by j. 3. i = *j + 1; i:10 j:20 k:30 * has higher precedence than +. Assigns to i the contents of the location pointed to by j, incremented by 1. 12 3 10 ?

  15. 4. k = &i; i:10j:20k:30 • printf (“%d”, *k); output: 3 3 10 10

  16. Example 2 int a=42, b=53, *p1, *p2; p1 = &a; p2 = p1; p2 = &b; p1? p2? *p1? *p2?

  17. int a=42, b=53, *p1, *p2; p1 p2 ? 42 53 ? p2 = p1; 42 p1 p2 p1 = &a; 53 42 p1 p2 53 ? p2 = &b; 42 p1 p2 53

  18. Example 3 int *p1, v1; v1 = 0; p1 = &v1; *p1 = 42; v1? p1? *p1? int a=8, b=9; int *p1, *p2; p1 = &a; p2 = &b; p1 = p2; vs. *p1 = *p2;

  19. p1 = p2; beforeafter 8 8 p1 p2 p1 p2 9 9 *p1 = *p2; beforeafter 8 9 p1 p2 p1 p2 9 9

  20. Example 4 # include <stdio.h> main( ) { int j, i, *intptr; scanf(“%d%d”, &i, &,j); intptr = i > j ? &i:&j; printf(“%d\n”, *intptr); } Address of the larger var is stored in ? : then the larger number is output.

  21. 3 7 Example 5 int a=3,b=7,*p; p = &b; a b p *p=2**p–a; printf (“b= %d\n”, b); “The object pointed to by p (i.e., b) is assigned the value of 2**p–a.” 1) 2 * *p 2 * 7 2) 14 – 3 11 3) Which is assigned? b 11

  22. Pointer Initialization int i, *p = &i; correct int *p = &i, i; sequence wrong. The variable must be defined before the address can be assigned.

  23. p = &i; p “points to” i p = 0; “Null” is usually defined as 0. p = NULL; Pointer constant points “nowhere”. p = (int *) 1307; cast to pointer to int. 1307 is an absolute address in memory.

  24. 3 5 int i = 3, j = 5, *p = &i, *q = &j, *r; double x; r ? i:10j p:50q x 1) p = i + 7; The only integer value that can be assigned to a pointer variable directly is the special value 0 (NULL). To assign any other value requires a cast(int *) (i + 7) ? ILLEGAL

  25. 1 • **&p All are unary operators. &p - The address of p (50). *&p - “The Addr stored at p” (10) i:10p:5 **&p – The contents of the address (*&p) “The contents of the variable pointed to by p” i.e., 3 2 3 10 3

  26. 3) r = &x;Illegal Why? x is a double variable r is pointer to int. 4) 7 * *p / *q + 7i:10 j Dereference Right to Leftp q 1. *q 5 2. *p 3 3. 7 * 3 [21] 4. 21/5 4 5 4 + 7 11 3 5

  27. 5) *(r = &j) *= *p 4 2 1 5 3 j - int var p - pointer to int j i r - pointer to int r p 1. &j - Address of j 2. r = r points to j 3. *p contents of thing pointed to by p i.e., 3 4. *( ) Location pointed to by r, i.e., j. 5. *= *r *= *p; *r = *r * *p; 3 5 1 1

  28. Pointer Arithmetic int *v=(int *)100; Byte Address. 100 v 102 v + 1 104 v + 2 106 v + 3 108 v + 4 assume int are 2 bytes long.

  29. char *ch=(char *)3000;int *i=(int *)3000; Pointer Arithmetic (cont.) ch ch+1 ch+2 ch+3 ch+4 ch+5 3000 i i+1 i+2 3001 3002 3003 3004 3005

  30. Pointer Arithmetic (cont.) • Only two operations are allowed. • Addition and subtraction. e.g. p++; p--; p1=p1+12; • Cannot multiply or divide pointers. • Cannot add two pointers. • Cannot add or subtract type float or double to or from pointers.

  31. Call by Reference • Passing a ADDRESS of the argument to a formal parameter which is a POINTER of the same type as the argument. • Code within the function can change the value of the argument passed to thefunction.

  32. Steps • Declare a function parameter to be a pointer. 2) Pass an address as an argument when the function is called. 3) Use the dereferenced pointer in the function body to reference the argument in the calling function.

  33. #include <stdio.h>void swap(int *x, int *y); int main (void){  int i=10, j=20;   printf("i and j before swapping: %d %d\n", i, j);  swap(&i, &j); /* pass the addresses of i and j */   printf("i and j after swapping: %d %d\n", i, j);   return 0;}

  34. void swap(int *x, int *y) {  int temp;   temp=*x; /* save the value at address x */  *x =*y;    /* put y into x */  *y=temp;  /* put x into y */}

  35. Storage Classes

  36. Every variable or function in C has 2 attributes: TypeStorage Class General form: storage-specifier  type var_name Storage Class : determines how the compiler allocates memory to that variable. Auto Extern Register Static

  37. Storage Class of a C object defines its: • Spatial Territory- Defines where it can be referred to in the program. Its Scope or Visibility. • Temporal Authority- Defines when an object is available. The Extent to which it exists.

  38. Auto Variables The storage class auto can only be used in a block: it cannot be used at the global level. • Spatial Limitation(scope): The auto variable is limited to the block in which it is defined and its sub-blocks. LOCAL. • Temporal Limitation(extent): Are alive only when the block in which they are defined is running. Variable does not exist before block execution, when block terminates it dies(mortal).

  39. Impact of Auto • Auto is the default SC for local vars because it makes the most efficient use of memory. • Good programmers use Auto when ever possible because their lifetime is brief and their scope islimited to one function. • They provide an effective way to isolate theactions of each program unit from the others.

  40. auto • Most common of the four storage classes. • Variables declared within function bodies are auto by default. • Thus auto is rarely explicitly specified. auto int a,b,c; auto float f;

  41. Unintentional interference between functions is minimized and, making it much easier to debug large programs. Constant allocation and deallocation of auto vars does add some overhead to the the execution time, but since is done quite efficiently, the time wasted is minimal and the advantages of the added modularity are great.

  42. When a block is entered, the system allocates memory for the auto variables. “LOCAL” i.e., Those declared in the block. When Block is exited the variables are released and their values lost. IF re-enter the Block? _________ Memory is reallocated.

  43. Call by Reference with Auto Parameters 1) When the block is suspended, such as when it calls another function,the variable is suspended but can still be accessed andchanged(e.g. actual argument in a func call). 2) It can still be referenced and changed through a pointer to it. When the block becomes active again, the variable becomes active i.e., can directly reference it by name.)

  44. Extern • Primary purpose is to make variables visible to other compilation units. • Must be defined in the global area of a program, that is outside of any block. • All global variables( i.e., those defined outside any function) are EXTERN by default. !!!!! EXTERN variables are DANGEROUS and should be used with CAUTION!!!!!

  45. Extern Characteristics • Spatial Limitation(global): It is visible from itsdefinition to the end of the program. It can be referred to and changed by all blocks that come after it as well as by other programs that are aware of its existence. • Temporal Limitation(Immortal): The whole lifeof the program. From the time of its allocation till the end of the program.

  46. extern (cont.) Variables declared outside of a function have extern storage class by default. i.e., even if don’t use keyword extern. Extern Var’s are initialized to 0 automatically. extern - “Look for the variable elsewhere – either in this or another file.”

  47. See 3 handouts on program examples Relevant to EXTERN storage class. K-5, K-6, K-7.

  48. Register Storage Class Variables so declared should be stored in high speed memory i.e., Registers(cpu) provided if is possible to do so. Defaults to Auto if register not available

  49. Typically, the compiler has only a few suchregisters available. Many are required for system use & can’t be allocated otherwise: * Base Addr of program * Return Addr after pgm execution ends, * Instruction reg, program counter, etc……..

  50. It is an attempt to improve execution speed. Most frequently accessed variable – loop control variable or function parameters. register int i; for (i = d, i <LIMIT, ++i){ … } If storage class is present & Type is absent - get int. register i ;

More Related