Chapter 11
Download
1 / 18

Programming in C Chapter 11 - PowerPoint PPT Presentation


  • 468 Views
  • Updated On :

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

Related searches for Programming in C Chapter 11

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Programming in C Chapter 11' - benjamin


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Chapter 11

Chapter 11

Introduction to Programming in C


Pointers
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*/


Pointers1
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


Pointers2
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


Pointers3
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 */


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


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


Pointers6
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. */


Pointers7
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
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);


Pointers8
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
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 pointers1
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 pointers2
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
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 functions1
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 functions2
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
Homework

P. 218, 1, 2, 3, 4


ad