C Pointer Parameters: Passing Arrays, Strings & const Memory Safety
Welcome to Phase 10 (Chapter 25): C Pointer Parameters β Passing Arrays, Strings & const Memory Safety Masterclass! When passing massive datasetsβsuch as 50,000 sensor readings or large text paragraphsβinto functions, pointers eliminate memory copying overhead. However, giving functions raw pointer access to caller memory creates the risk of accidental data corruption. In this exhaustive textbook-grade guide, you will master passing arrays and strings via pointer parameters, learn how to enforce iron-clad read-only memory safety using const pointer qualifiers, and discover how functions can return multiple results simultaneously via Output Pointer Parameters.
When passing text to a function, you must strictly declare your intent using the const qualifier:
| Parameter Signature | Intent & Capability | Can Mutate Caller String? | Example Use Case |
|---|---|---|---|
void process(char* str) |
Mutator: Modifies text in-place in RAM. | β YES | toUpper(), reverse(), trim() |
void inspect(const char* str) |
Inspector: Read-only parsing/searching. | β NO (Compiler Protected) | strlen(), strcmp(), print() |
In C, the return statement can only send back a single primitive value.
How can a function compute and return Minimum, Maximum, and Average in a single execution pass?
By using Output Pointer Parameters:
#include <stdio.h>
// Function returns 3 outputs simultaneously via pointer parameters!
void getArrayStats(const int *arr, int size, int *minOut, int *maxOut, double *avgOut) {
int min = arr[0], max = arr[0], sum = 0;
for (int i = 0; i < size; i++) {
if (arr[i] < min) min = arr[i];
if (arr[i] > max) max = arr[i];
sum += arr[i];
}
// Populating caller's RAM memory slots via pointer dereferencing!
*minOut = min;
*maxOut = max;
*avgOut = (double)sum / size;
}
int main(void) {
int grades[] = {88, 92, 79, 95, 84};
int size = sizeof(grades) / sizeof(grades[0]);
int minGrade, maxGrade;
double avgGrade;
// Passing addresses &minGrade, &maxGrade, &avgGrade
getArrayStats(grades, size, &minGrade, &maxGrade, &avgGrade);
printf("Minimum: %d | Maximum: %d | Average: %.2f\n", minGrade, maxGrade, avgGrade);
return 0;
}
Q1: Why should all read-only pointer parameters use const?
1. Memory Safety: It prevents accidental assignment bugs.
2. Compatibility: It allows passing string literals (which live in read-only memory) without compiler warnings.
3. Compiler Optimization: It enables the compiler to optimize register caching knowing the underlying memory will not mutate.
Q2: What is the difference between const int *p and int * const p as function parameters?
const int *p protects the caller's data from being modified. int * const p prevents the local pointer variable from being reassigned to another address inside the function.
Run this quotient and remainder calculator in our live GCC compiler:
#include <stdio.h>
void divide(int dividend, int divisor, int *quotient, int *remainder) {
if (divisor != 0) {
*quotient = dividend / divisor;
*remainder = dividend % divisor;
}
}
int main(void) {
int q, r;
divide(29, 5, &q, &r);
printf("29 / 5 = %d (Remainder: %d)\n", q, r);
return 0;
}