220 likes | 772 Vues
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
E N D
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 General form of pointer variable declaration: type *var-name Example: int *p; /* p is a pointer to an integer*/
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
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
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 */
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.
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.
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. */
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?*/ }
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);
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? */
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 */
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.
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.
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?
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?
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?
Homework P. 218, 1, 2, 3, 4