Last active
November 9, 2015 23:24
-
-
Save bzdgn/5bfa94a2faa87baf40ea to your computer and use it in GitHub Desktop.
Pointer to Pointers Sample & Out Parameter Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/******************************************** | |
* Function : MinMax * | |
* Input parameters : *begin, *end * | |
* Output parameters: **smallest, **largest * | |
********************************************/ | |
int minMax(int * begin, | |
int * end, | |
int ** smallest, | |
int ** largest) | |
{ | |
printf("[minMax]...minMax function started\n"); | |
if(begin && end) | |
{ | |
if (begin == end) | |
{ | |
*smallest = 0; | |
*largest = 0; | |
printf("[minMax]...No range, function halted\n"); | |
return -1; | |
} | |
else | |
{ | |
*smallest = *largest = begin; | |
for(; begin != end; ++begin) | |
{ | |
if( *begin < **smallest) | |
{ | |
*smallest = begin; | |
} | |
if( *begin > **largest) | |
{ | |
*largest = begin; | |
} | |
} | |
} | |
} | |
else | |
{ | |
printf("[minMax]...input parameters must not be NULL, function halted\n"); | |
return -1; | |
} | |
printf("[minMax]...minMax function ended\n"); | |
return 0; | |
} | |
/******************************************** | |
* Function : printArray * | |
* Input parameters : *array * | |
* Output parameters: N/A * | |
********************************************/ | |
void printArray(int *array, int size) | |
{ | |
if(array) | |
{ | |
printf("array : "); | |
for(int i = 0; i != size; i++) | |
{ | |
printf("%2d ", array[i]); | |
} | |
printf("\n"); | |
} | |
else | |
{ | |
printf("[printArray]...array is NULL, function halted\n"); | |
} | |
} | |
int main() | |
{ | |
printf("****************************************************\n"); | |
int value = 123; /* an integer */ | |
int *p = &value; /* pointer pointing to an int */ | |
int **pp = &p; /* pointer to pointer which is pointing to an int */ | |
printf("integer value : %d\n", value); | |
printf("content of pointer : %d\n", *p); | |
printf("content thru the pointer pointer : %d\n", *(*pp)); | |
printf("double dereferencing of pp : %d\n", **pp); | |
printf("****************************************************\n"); | |
int values[] = { 5, 1, 3, 2, 9, 0 }; | |
int size = sizeof(values) / sizeof(values[0]); | |
printArray(values, size); | |
int * smallest = 0; | |
int * largest = 0; | |
int err = minMax( values, | |
values + size, | |
&smallest, | |
&largest); | |
if(!err) | |
{ | |
printf("Smalest number in the array : %d\n", *smallest); | |
printf("Largest number in the array : %d\n", *largest); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment