1 / 18

Chapter 11

Chapter 11. Introduction to Programming in C. Pointers. Pointer: A variable that contains a memory address - Address is often the location of another variable - This allows for dynamic memory location, linked lists - If x contains the address of y , it is said that x points to y

benjamin
Download Presentation

Chapter 11

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. Chapter 11 Introduction to Programming in C

  2. Pointers Pointer: A variable that contains a memory address - Address is often the location of another variable - This allows for dynamic memory location, linked lists - If x contains the address of y, it is said that x points to y General form of pointer variable declaration: type *var-name Example: int *p; /* p is a pointer to an integer*/

  3. Pointers • The indirection operator * • Should be read as “a pointer to…” • Ex: “int *p” reads “p is a pointer to an integer” int *p; /* Pointer to an integer */ int j = 1; /* Integer j holds the value of 1 */ j p 1

  4. Pointers The address operator &, applied to an object in an expression, produces a pointer to that object int *p; /* Pointer to an integer */ int j = 1; /* Integer j holds the value of 1 */ p = &j; /* p gets the address of j */ printf(“%d”, j); /* Outputs 1 */ printf(“%d”, *p); /* Outputs 1 */ j p 1

  5. Pointers A pointer variable can be declared with other variables: int i, j, a[10], *p, *q; A pointer variable can only point to a particular type: int *p; /* Can only point to an int */ float *q; /* Can only point to an float */ char *r; /* Can only point to an char */

  6. Pointers The base type of a pointer is important. int *p; // p points to an integer double f; // f is type float //…. p = &f; // p gets the address of f (?) This does not work, because p points to an integer and f is not an integer. This is equivalent to trying to assign a float to an integer. WILL NOT COMPILE.

  7. Pointers The base type of a pointer is important. int *p; // p points to an integer double f; // f is type float //…. p = (int *) &f; // Now it would work. Integer pointer p has been assigned the address of x, which is a double. So when y is assigned the value pointed to by p, y only receives four bytes of data, not the eight required for a double.

  8. Pointers Examples: float *balptr; /* balptr is a pointer to a float */ balptr = &balance; This puts into balptr the memory address of variable balance. “&” returns the address of the variable it precedes. “*” is the complement of “&” “*” returns the value of the variable located at the address specified by the operand: value = *balptr; /* float variable “value” now has the value of whatever was held in the location balptr points to. */

  9. Pointers void main() { int balance; int *balptr; int value; balance = 3200; balptr = &balance; value = *balptr; printf("Balance is: %d\n", value); printf(" %d\n", *balptr); /*What does this do?*/ }

  10. Pointers to Pointers What would this do? int value = 10; int *i; // Pointer to an integer int **j; // Pointer to a pointer to an integer int ***k; // Pointer to a pointer to a pointer to an integer i = &value; printf(“ %d \n”, *i); j = &i; printf(“ %d \n”, **j); k = &j; printf(“ %d \n”, ***k);

  11. Pointers++ What would this do? int *p, value; p = &value; *p = 100;/* Assigns value 100 to location pointed to by p */ printf("%d\n", value); /* What does this do? */ (*p)++;/* Increment the value at that location by 1 */ printf("%d\n", value); /* What does this do? */ (*p)--;/* Increment the value at that location by 1 */ printf("%d\n", value); /* What does this do? */

  12. Pointers and Pointers int i, j, *p, *q; p = &i; /* Copies address of i to p */ q = p; /* Copies p, the address of i, to q */ Note: Now both p and q point to i. If we assign a value to i, *p, or *q we can modify it. i = 1; *p = 1; *q = 1; /* All do the same thing */

  13. Pointers and Pointers int i, j, *p, *q; p = &i; /* Copies address of i to p */ q = p; /* Copies p, the address of i, to q */ Be careful not to confuse p = q; with *p = *q; The first statement is a pointer assignment, the second isn’t.

  14. Pointers and Pointers int i, j, *p, *q; p = &i; /* Copies address of i to p */ q = &j; /* Copies address of j to q */ i = 1; *q = *p; Note that this is NOT a pointer assignment.

  15. Pointers and Functions void swap (int x, int y); main() { int i, j; i = 1; j = 2; swap(i, j); printf( "i = %d and j = %d\n", i, j); return 0; } void swap (int x, int y) {int temp; temp = x; x = y; y = temp; } Will this work?

  16. Pointers and Functions void swap (int *x, int *y); main() { int i, j; i = 1; j = 2; swap(&i, &j); printf( "i = %d and j = %d\n", i, j); return 0; } void swap (int *x, int *y) {int *temp; temp = x; x = y; y = temp; } Will this work?

  17. Pointers and Functions void swap (int *x, int *y); main() { int i, j; i = 1; j = 2; swap(&i, &j); printf( "i = %d and j = %d\n", i, j); return 0; } void swap (int *x, int *y) {int temp; temp = *x; *x = *y; *y = temp; } Will this work?

  18. Homework P. 218, 1, 2, 3, 4

More Related