1 / 10

Pointers

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 >

Télécharger la présentation

Pointers

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. Pointers Value, Address, and Pointer

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

  3. 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); • }

  4. Pointers int *p, *q; p = &x; x q = &y; y z = *p + *q + 3; z values of p and q • p is 70000 • q is 70004 p q addresses of p and q • &p is 70012 • &qis 70016 values pointed by p and q • *p is 5 • *q is 10

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

  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 <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); • }

  8. 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); • }

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

  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

More Related