C Pointer Parameters: Passing Arrays, Strings & const Memory Safety

⚑ C (C17 / C23 Standard) 🟒 Lesson 25 πŸ“‚ Phase 10: Pointers and Functions πŸ“… 2026 Comprehensive Master Edition
πŸ“Œ Covered in this in-depth guide: Passing 1D/2D Arrays to Functions Β· Passing Strings (char* vs const char*) Β· const Pointer Parameters Β· Returning Multiple Values via Output Pointers

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.

1Passing Strings: Mutable char* vs Read-Only const char*

When passing text to a function, you must strictly declare your intent using the const qualifier:

Parameter SignatureIntent & CapabilityCan 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()
2Returning Multiple Results via Output Pointer Parameters ⭐

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:

C β€” Output Pointer Parameters (Multiple Return Values) β–Ά Run Code in C Compiler
#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;
}
3Frequently Asked Questions & Technical Interview Deep-Dive

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.

πŸ’» Try It Yourself β€” Test Output Pointers in Online C Compiler

Run this quotient and remainder calculator in our live GCC compiler:

C (GCC Standard) β–Ά Open C 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;
}
Open in Online C Compiler β†’