SlideServe
Menu
Préférences
© SlideServe · v2026

Part 1 B

eileen avatar
4 diapositives · 147 Views · Oct 13, 2014

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

Part 1 B - slide 1
01 / 4Part 1 B
1 / 17

Télécharger la présentation

Part 1 B
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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; • }