Understanding Function Parameters in C Language

Differences Between Pointer Parameters and Regular Parameters

In C language, there are mainly two ways to pass function parameters: pass by value (regular parameters) and pass by pointer (pointer parameters).

Pass by Value (Regular Parameters)

  • The function receives a copy of the actual parameter
  • Modifications to the parameter do not affect the original data
  • Suitable for basic data types and small structures

Pass by Pointer (Pointer Parameters)

  • The function receives the memory address of the actual parameter
  • Can directly modify the original data through the pointer
  • Suitable for large data structures, arrays, and situations where multiple values need to be modified

Advanced Usage of Pointer Parameters

1. Modifying Multiple Values

A C language function can only return one value, but multiple external variables can be modified through pointer parameters.

#include <stdio.h>

void calculate(int a, int b, int *sum, int *product) {
    *sum = a + b;
    *product = a * b;
}

int main() {
    int x = 5, y = 3;
    int s, p;
    calculate(x, y, &s, &p);
    printf("Sum: %d, Product: %d\n", s, p);
    return 0;
}

2. Dynamic Memory Allocation

Return dynamically allocated memory through pointer parameters.

#include <stdio.h>
#include <stdlib.h>

void createArray(int **arr, int size) {
    *arr = (int*)malloc(size * sizeof(int));
    if (*arr != NULL) {
        for (int i = 0; i < size; i++) {
            (*arr)[i] = i * 2;
        }
    }
}

int main() {
    int *myArray = NULL;
    int size = 5;
    createArray(&myArray, size);
    
    if (myArray != NULL) {
        for (int i = 0; i < size; i++) {
            printf("%d ", myArray[i]);
        }
        free(myArray);
    }
    return 0;
}

3. String Handling

Strings in C language are passed as character pointers.

#include <stdio.h>
#include <string.h>

void reverseString(char *str) {
    int length = strlen(str);
    for (int i = 0; i < length / 2; i++) {
        char temp = str[i];
        str[i] = str[length - i - 1];
        str[length - i - 1] = temp;
    }
}

int main() {
    char text[] = "Hello, World!";
    printf("Original: %s\n", text);
    reverseString(text);
    printf("Reversed: %s\n", text);
    return 0;
}

4. Function Pointers as Parameters

Functions can be passed as parameters to other functions.

#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

void calculate(int a, int b, int (*operation)(int, int)) {
    int result = operation(a, b);
    printf("Result: %d\n", result);
}

int main() {
    calculate(10, 5, add);
    calculate(10, 5, subtract);
    return 0;
}

5. Passing Multidimensional Arrays

Passing multidimensional arrays to functions.

#include <stdio.h>

void printMatrix(int rows, int cols, int matrix[rows][cols]) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    printMatrix(2, 3, matrix);
    return 0;
}

6. Struct Pointers

Passing and modifying structures through pointers.

#include <stdio.h>
#include <string.h>

typedef struct {
    char name[50];
    int age;
    float salary;
} Person;

void updateSalary(Person *p, float newSalary) {
    p->salary = newSalary;
}

void printPerson(const Person *p) {
    printf("Name: %s, Age: %d, Salary: %.2f\n", p->name, p->age, p->salary);
}

int main() {
    Person john = {"John Doe", 30, 50000.0};
    printPerson(&john);
    updateSalary(&john, 55000.0);
    printPerson(&john);
    return 0;
}

Conclusion

Pointer parameters provide powerful capabilities in C language, allowing functions to:

  1. Directly modify the caller’s data
  2. Efficiently handle large data structures
  3. Implement dynamic memory management
  4. Create flexible callback mechanisms

In contrast, regular parameters can only pass copies of values and cannot modify the original data. Proper use of pointer parameters can greatly enhance the efficiency and flexibility of programs.

Leave a Comment