Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers Value, Address, and Pointer. Values and Addresses...... 70000 70004 70008 70012 70016 70020 70024 70028...... int x, y, z; y x z values of x,

Similar presentations


Presentation on theme: "Pointers Value, Address, and Pointer. Values and Addresses...... 70000 70004 70008 70012 70016 70020 70024 70028...... int x, y, z; y x z values of x,"— Presentation transcript:

1 Pointers Value, Address, and Pointer

2 Values and Addresses...... 70000 70004 70008 70012 70016 70020 70024 70028...... int x, y, z; y x 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 x = 5; y = x + 5; z = y + 5;

3 Example 1 #include 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); }

4 Pointers...... 70000 70004 70008 70012 70016 70020 70024 70028...... int *p, *q; y x z 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

5 Example 2 #include 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); }

6 Example 3 int 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);

7 Example 4 – Scope of variable #include 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); }

8 Example 5 – Scope of variable #include 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); }

9 Sort Problem: Sort three numbers in ascending order Analysis: Put minimum in 1 st, next minimum in 2 nd Design: – Compare 1 st &2 nd, put smaller in 1 st, – Compare 1 st & 3 rd, put smaller in 1 st, – Compare 2 nd & 3 rd, put smaller in 2 nd – 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.

10 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


Download ppt "Pointers Value, Address, and Pointer. Values and Addresses...... 70000 70004 70008 70012 70016 70020 70024 70028...... int x, y, z; y x z values of x,"

Similar presentations


Ads by Google