Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable 100.00 cost:1024.

Similar presentations


Presentation on theme: "Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable 100.00 cost:1024."— 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 100.00 cost:1024 cost_ptr:2048 double cost = 100.00; double *cost_ptr = &cost;

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

4 Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable 100.00 1024 cost:1024 cost_ptr:2048 double cost = 100.00; double *cost_ptr = &cost; 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);

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

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 5 a:1036 1 b:1032 main swap 5 a:1028 1 b:1024 1 a:1028 5 b:1024

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

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 1 a:1036 5 b:1032 main swap a:1028 b:1024 a:1028 b:1024

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


Download ppt "Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable 100.00 cost:1024."

Similar presentations


Ads by Google