pointers

10
Pointers Value, Address, and Pointer

Upload: phyllis-martinez

Post on 01-Jan-2016

18 views

Category:

Documents


1 download

DESCRIPTION

Pointers. Value, Address, and Pointer. Values and Addresses. int x, y, z;. x = 5;. x. y = x + 5;. y. z = y + 5;. z. values of x, y, and z x is 5 y is 10 z is 15. addresses of x, y, and z &x is 70000 &y is 70004 &z is 70008. Example 1. #include < stdio.h > - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Pointers

Pointers

Value, Address, and Pointer

Page 2: Pointers

Values and Addresses...

51015

.

.

.

.

.

.

70000

70004

70008

70012

70016

70020

70024

70028...

int x, y, z;

y

x

z

.

.

.

510

.

.

.

.

.

.

5

.

.

.

.

.

.

.

.

.

values of x, y, and z x is 5 y is 10 z is 15

addresses of x, y, and z &x is 70000 &y is 70004 &z is 70008

x = 5;

y = x + 5;

z = y + 5;

Page 3: Pointers

Example 1#include <stdio.h>

int main(void) {int x, y, z;

x = 5;y = 10;z = x + y;

printf("The value of x is: %3d\n", x);printf("The value of y is: %3d\n", y);printf("The value of z is: %3d\n", z);printf("\n");printf("The address of x is: %d\n", &x);printf("The address of y is: %d\n", &y);printf("The address of z is: %d\n", &z);

return(0);}

Page 4: Pointers

Pointers...

51018

7000070004

.

.

.

.

.

.

70000

70004

70008

70012

70016

70020

70024

70028...

int *p, *q;

y

x

z

.

.

.

51015

7000070004

.

.

.

.

.

.

51015

70000

.

.

.

.

.

.

51015

.

.

.

values of p and q

p = &x;

q = &y;

z = *p + *q + 3;

p

q

p is 70000 q is 70004

addresses of p and q &p is 70012 &q is 70016

values pointed by p and q *p is 5 *q is 10

Page 5: Pointers

Example 2#include <stdio.h>

int main(void) {int x, y, z;int *p, *q;

x = 5;y = 10;

p = &x;q = &y;

z = *p + *q + 3;

printf("The address of x is: %d\n", &x);printf("The address of y is: %d\n", &y);printf("\n");printf("The value of p is: %3d\n", p);printf("The value of q is: %3d\n", q);printf("\n");printf("The value of x is: %3d\n", x);printf("The value of y is: %3d\n", y);printf("\n");printf("The value of x is: %3d\n", *p);printf("The value of y is: %3d\n", *q);printf("\n");printf("The value of z is: %3d\n", z);

return(0);}

Page 6: Pointers

Example 3int x, y;int *px, *py;

x = 3;y = 5;

px = &x;py = &y

printf(“%d”, *px);

*px = 1;printf(“%d”, *px);

*py = 7;py = px;printf(“%d”, *px);

Page 7: Pointers

Example 4 – Scope of variable#include <stdio.h>

void DoIt(int x);

int main(void) {int x;

x = 3;printf("Before calling function: %d\n", x);DoIt(x);printf("After calling function: %d\n", x);

return(0);}

void DoIt(int x) {x = 44;printf("Inside the function: %d\n", x);

}

Page 8: Pointers

Example 5 – Scope of variable#include <stdio.h>

void DoIt(int *x);

int main(void) {int x;

x = 3;printf("Before calling function: %d\n", x);DoIt(&x);printf("After calling function: %d\n", x);

return(0);}

void DoIt(int *x) {*x = 44;printf("Inside the function: %d\n", *x);

}

Page 9: Pointers

Sort

• Problem: Sort three numbers in ascending order• Analysis: Put minimum in 1st, next minimum in 2nd • Design:

– Compare 1st &2nd, put smaller in 1st, – Compare 1st & 3rd, put smaller in 1st,– Compare 2nd & 3rd, put smaller in 2nd – Use function for sort called MySort takes three pointers

and sorts the contents. Does not return a value– Use a function for exchanging two values called xChange

which takes two pointers and exchanges the contents.

Page 10: Pointers

Sort

• Code:• Test:– Try 1 2 3 output 1 2 3– Try 3 2 1 output 1 2 3– Try 5 8 2 output 2 5 8– Try 8 2 5 output 2 5 8

• Maintain: Correct errors, make improvements, add new functionality