Recap: Function Pointer Args
Part of the Logic & Flow section of Coddy's C journey — lesson 30 of 63.
Challenge
EasyWrite a C program that implements a function to find both the minimum and maximum values in an array using pointer arguments. Your program should:
- Create a function named
findMinMaxthat takes four parameters:- An integer array
- The size of the array
- A pointer to an integer for storing the minimum value
- A pointer to an integer for storing the maximum value
- Inside the
findMinMaxfunction, iterate through the array to find the minimum and maximum values - Use the dereference operator to store the minimum value at the address pointed to by the min pointer
- Use the dereference operator to store the maximum value at the address pointed to by the max pointer
- In the
mainfunction, read an integernrepresenting the number of elements in the array - Read
ninteger values and store them in an array - Declare two integer variables
minValueandmaxValueto store the results - Call the
findMinMaxfunction, passing the array, its size, and the addresses ofminValueandmaxValue - Print the results in the format:
Minimum: [value]andMaximum: [value]
For example, if the input is:
5
12 7 23 4 18Your output should be:
Minimum: 4
Maximum: 23This challenge demonstrates the powerful technique of using multiple pointer arguments to return multiple values from a single function call, allowing the function to modify variables in the calling function's scope.
Try it yourself
#include <stdio.h>
// TODO: Write your findMinMax function here
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int minValue, maxValue;
// TODO: Call the findMinMax function here
printf("Minimum: %d\n", minValue);
printf("Maximum: %d\n", maxValue);
return 0;
}All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions