1 / 12

Pointers and Output Parameters

Pointers and Output Parameters. Pointers. A pointer contains the address of another memory cell i.e., it “points to” another variable. double cost = 100.00;. cost:1024. 100.00. double *cost_ptr = &cost;. cost_ptr:2048. Pointers. A pointer contains the address of another memory cell

Download Presentation

Pointers and Output Parameters

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 and Output Parameters

  2. Pointers • A pointer contains the address of another memory cell • i.e., it “points to” another variable double cost = 100.00; cost:1024 100.00 double *cost_ptr = &cost; cost_ptr:2048

  3. Pointers • A pointer contains the address of another memory cell • i.e., it “points to” another variable double cost = 100.00; cost:1024 100.00 double *cost_ptr = &cost; cost_ptr:2048 1024

  4. Pointers • A pointer contains the address of another memory cell • i.e., it “points to” another variable double cost = 100.00; printf(“cost: %lf”, cost); printf(“cost_ptr: %d”, cost_ptr); printf(“&cost: %d”, &cost); printf(“&cost_ptr: %d”, &cost_ptr); printf(“*cost_ptr: %lf”, *cost_ptr); cost:1024 100.00 double *cost_ptr = &cost; cost_ptr:2048 1024

  5. Pointers • A pointer contains the address of another memory cell • i.e., it “points to” another variable double cost = 100.00; cost: 100.00 cost_ptr: 1024 &cost: 1024 &cost_ptr: 2048 *cost_ptr: 100.00 cost:1024 100.00 double *cost_ptr = &cost; cost_ptr:2048 1024

  6. Call By Value int main(void) { int a = 5, b = 1; swap(a, b); printf(“a=%d, b=%d”, a, b); return (0); } void swap(int a, int b) { int tmp = a; a = b; b = tmp; printf(“a=%d, b=%d”, a, b); }

  7. Call By Value int main(void) { int a = 5, b = 1; swap(a, b); printf(“a=%d, b=%d”, a, b); return (0); } void swap(int a, int b) { int tmp = a; a = b; b = tmp; printf(“a=%d, b=%d”, a, b); } Prints: a=1, b=5 a=5, b=1

  8. Call By Value a:1036 5 b:1032 1 main a:1028 5 a:1028 1 b:1024 1 b:1024 5 swap

  9. Call By Reference a:1036 5 b:1032 1 main a:1028 b:1024 swap

  10. Call By Reference int main(void) { int a = 5, b = 1; swap(&a, &b); printf(“a=%d, b=%d”, a, b); return (0); } void swap(int *a, int *b) { int tmp = *a; //follow the pointer to a, get the value, and store it in tmp *a = *b; *b = tmp; printf(“a=%d, b=%d”, *a, *b); }

  11. Call By Reference a:1036 1 b:1032 5 main a:1028 a:1028 b:1024 b:1024 swap

  12. fraction.c • Create a single function that will multiply a fraction.

More Related