1 / 4

Part 1 B

Part 1 B. Hannu Laine. Comparing pointer parameters and reference parameters. Pointer parameters. Reference parameters. //prototype of swap void swap(int *a, int *b); //application void main(void) { in number1 = 1, number2 = 2; swap(&number1, &number2);

eileen
Download Presentation

Part 1 B

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. Part 1 B Hannu Laine

  2. Comparing pointer parameters and reference parameters Pointer parameters Reference parameters • //prototype of swap • void swap(int *a, int *b); • //application • void main(void) { • in number1 = 1, number2 = 2; • swap(&number1, &number2); • printf("%d %d", number1, number2); • } • //implementation of swap • void swap(int *a, int *b) { • int aux; • aux = *a; • *a = *b; • *b = aux; • } • //prototype of swap • void swap(int &a, int &b); • //application • void main(void) { • in number1 = 1, number2 = 2; • swap(number1, number2); //pointers are • // passed instead of values • printf("%d %d", number1, number2); • } • //implementation of swap • void swap(int &a, int &b) { • int aux; • aux = a; //deferencing is used internally • a = b; //with a and b • b = aux; • } Back

  3. Returning a pointer and returning a reference Returning a pointer Returning a reference • //prototype of a function • int *getMax(int *arr, int n); • //application • int main(void) { • int maxValue; • int array[6] = {3, 5, 2, 8, 6, 7}; • printf (”%d”, *getMax(array, 6)); //Right value • maxValue = *getMax(array, 6); • *getMax (array, 6) = *getMax (array, 6) *10; • //Left value • } • //implementation getMax • int *getMax(int *arr, int n) { • int i, maxi = 0; • for (i = 1 ; i < n ; i++) • if (arr [ i ] > arr [ maxi ] ) • maxi = i; • return &arr[maxi]; • } • //prototype of a function • int &getMax(int *arr, int n); • //application • int main(void) { • int maxValue; • int array[6] = {3, 5, 2, 8, 6, 7}; • printf (”%d”, getMax(array, 6)); //Right value • maxValue = getMax(array, 6); • getMax(array, 6) = getMax(array, 6) *10; //Left value • } • //implementation getMax • int &getMax(int *arr, int n) { • int i, maxi = 0; • for (i = 1 ; i < n ; i++) • if (arr [ i ] > arr [ maxi ] ) • maxi = i; • return arr[maxi]; • } Back

  4. Why pointer parameters are need in C? Value parameters Pointer parameters //prototype of swap void swap(int a, int b); //application int main(void) { in number1 = 1, number2 = 2; swap(number1, number2); printf("%d %d", number1, number2); //the order is still 1 2 ! // swap does not work! } //implementation of swap void swap(int a, int b) { int aux; aux = a; //the contents are changed in the here a = b; b = aux; } • //prototype of swap • void swap(int *a, int *b); • //application • void main(void) { • in number1 = 1, number2 = 2; • swap(&number1, &number2); • printf("%d %d", number1, number2); • //the order 2 1 ! • // swap works correctly • } • //implementation of swap • void swap(int *a, int *b) { • int aux; • aux = *a; //the contents are changed in main • *a = *b; • *b = aux; • }

More Related