CoreTechED

Loading

Parameter Passing in C: Pass by Value & Pointers

Learn C parameter passing: pass by value and pass by pointers. Master function arguments, modify originals, and avoid common pitfalls.

Parameter Passing in C: Pass by Value & Pointers

Parameter Passing Techniques in C: A Comprehensive Tutorial

Parameter passing is the mechanism by which data is provided to functions when they are called. Understanding how parameters are passed in C is crucial for writing correct and efficient programs. C supports two main techniques: pass by value and pass by pointers (often called “call by pointers”). This tutorial provides an in-depth exploration of both techniques, their syntax, use cases, common pitfalls, and best practices.


Conceptual Introduction & Fundamentals

What are Parameters?

Parameters are variables used in function definitions to receive data from the caller. They come in two forms:

  • Formal Parameters (Parameters): Variables declared in the function definition that act as placeholders for incoming values.
  • Actual Parameters (Arguments): The real values or expressions passed to the function during a call.

Example:

c

int add(int a, int b) { // a and b are formal parameters return a + b; } int main(void) { int result = add(5, 3); // 5 and 3 are actual arguments return 0; }

Why Parameter Passing Matters

  • Data flow: Functions need input to perform their tasks.
  • Modularity: Parameters define clear interfaces between functions.
  • Efficiency: Different passing techniques have different performance characteristics.
  • Correctness: Understanding how data is passed prevents bugs (e.g., modifying originals unintentionally).
  • Memory management: Large data structures can be passed efficiently using pointers.

The Two Techniques

TechniqueDescriptionOriginal Modified?
Pass by ValueA copy of the argument is passedNo
Pass by PointersThe address of the argument is passedYes

Important Note: C does not have true “call by reference” like C++. Pass by pointers achieves similar effects by passing the address (which itself is passed by value).


Syntax & Core Mechanics

Pass by Value

In pass by value, the function receives a copy of the argument’s value. Changes to the parameter inside the function do not affect the original variable.

Syntax:

c

void function_name(type parameter) { // parameter is a copy }

Example:

c

#include <stdio.h> void func(int val) { val = 123; // Modifies only the local copy printf("Inside func: val = %d\n", val); } int main(void) { int x = 1; func(x); // Pass copy of x printf("Inside main: x = %d\n", x); // x unchanged return 0; }

Expected output:

text

Inside func: val = 123 Inside main: x = 1

Advantages:

  • Simple to understand and implement.
  • Original data is safe from accidental modification.
  • No pointer-related errors.

Disadvantages:

  • Inefficient for large arrays/structures (copies entire data).
  • Cannot modify the original variable.
  • Extra memory used for the copy.

Pass by Pointers

In pass by pointers, the function receives the memory address of the argument. By dereferencing the pointer, the function can access and modify the original variable.

Syntax:

c

void function_name(type *parameter) { // parameter is a pointer to the original }

Example:

c

#include <stdio.h> void func(int *val) { *val = 123; // Modify value at the address printf("Inside func: *val = %d\n", *val); } int main(void) { int x = 1; func(&x); // Pass address of x printf("Inside main: x = %d\n", x); // x modified return 0; }

Expected output:

text

Inside func: *val = 123 Inside main: x = 123

Advantages:

  • Efficient for large data (only address passed, not entire data).
  • Allows functions to modify original variables.
  • Enables returning multiple values from a function.

Disadvantages:

  • More complex and error-prone.
  • Risk of null pointer dereference, dangling pointers, and memory corruption.
  • Less type-safe if not careful.

Comparison

AspectPass by ValuePass by Pointers
Data passedCopy of valueMemory address
Original modified?NoYes (if dereferenced)
Memory overheadCopies entire dataOnly pointer size
ComplexitySimpleMore complex
Use caseSmall data, read-onlyLarge data, modification

Passing Arrays to Functions

Arrays are always passed by pointers (decay to pointers), even if you use array syntax.

c

#include <stdio.h> /* These two declarations are equivalent */ void printArray(int arr[], int size); void printArray2(int *arr, int size); void printArray(int arr[], int size) { for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); } void printArray2(int *arr, int size) { for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); } int main(void) { int numbers[] = {1, 2, 3, 4, 5}; printArray(numbers, 5); printArray2(numbers, 5); return 0; }

Expected output:

text

1 2 3 4 5 1 2 3 4 5

Passing Structures to Functions

Structures can be passed by value or by pointer.

By value (copies entire structure):

c

#include <stdio.h> struct Point { int x, y; }; void printPoint(struct Point p) { printf("(%d, %d)\n", p.x, p.y); p.x = 100; // Modifies local copy only } int main(void) { struct Point pt = {10, 20}; printPoint(pt); // Copy passed printf("After call: (%d, %d)\n", pt.x, pt.y); // Unchanged return 0; }

Output:

text

(10, 20) After call: (10, 20)

By pointer (efficient for large structures):

c

#include <stdio.h> struct Point { int x, y; }; void movePoint(struct Point *p, int dx, int dy) { p->x += dx; p->y += dy; } int main(void) { struct Point pt = {10, 20}; movePoint(&pt, 5, -3); printf("After move: (%d, %d)\n", pt.x, pt.y); return 0; }

Output:

text

After move: (15, 17)

Full Practical Code Example with Explanation

This program demonstrates both parameter passing techniques in a realistic scenario: a simple banking system with account operations that use pass by value and pass by pointers.

Program: Bank Account Manager

c

/** * bank_account.c * Demonstrates pass by value and pass by pointers. * Manages bank accounts with deposit, withdrawal, and transfer functions. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #define MAX_NAME_LEN 50 #define MAX_LINE_LEN 100 /* Structure for bank account */ typedef struct { char owner[MAX_NAME_LEN]; double balance; int accountNumber; } Account; /* Function prototypes */ void initializeAccount(Account *acc, const char *owner, int accountNumber, double initialDeposit); void deposit(Account *acc, double amount); /* Pass by pointer: modifies account */ int withdraw(Account *acc, double amount); /* Pass by pointer: modifies account */ double getBalance(const Account *acc); /* Pass by pointer: read-only */ void transfer(Account *from, Account *to, double amount); /* Pass by pointers */ void printAccount(const Account *acc); /* Pass by pointer: read-only */ void demonstratePassByValue(int x); /* Pass by value */ int readDouble(const char *prompt, double *outValue); int main(void) { Account account1, account2; double amount; int choice; char buffer[MAX_LINE_LEN]; /* Initialize accounts using pass by pointer */ initializeAccount(&account1, "Alice Johnson", 1001, 5000.0); initializeAccount(&account2, "Bob Smith", 1002, 3000.0); printf("=== Bank Account Manager ===\n"); printf("Account 1: %s (Balance: $%.2f)\n", account1.owner, account1.balance); printf("Account 2: %s (Balance: $%.2f)\n\n", account2.owner, account2.balance); /* Demonstrate pass by value */ printf("--- Pass by Value Demonstration ---\n"); int originalValue = 42; printf("Before function: originalValue = %d\n", originalValue); demonstratePassByValue(originalValue); printf("After function: originalValue = %d (unchanged)\n\n", originalValue); /* Menu-driven operations */ while (1) { printf("--- Menu ---\n"); printf("1. Deposit to Account 1\n"); printf("2. Withdraw from Account 1\n"); printf("3. Transfer from Account 1 to Account 2\n"); printf("4. Display Accounts\n"); printf("5. Exit\n"); printf("Enter choice: "); fflush(stdout); if (fgets(buffer, sizeof(buffer), stdin) == NULL) { fprintf(stderr, "Error reading choice.\n"); return EXIT_FAILURE; } choice = atoi(buffer); switch (choice) { case 1: if (readDouble("Enter deposit amount: $", &amount) != 0) { fprintf(stderr, "Invalid amount.\n"); break; } deposit(&account1, amount); /* Pass by pointer */ printf("Deposited $%.2f to Account 1.\n", amount); break; case 2: if (readDouble("Enter withdrawal amount: $", &amount) != 0) { fprintf(stderr, "Invalid amount.\n"); break; } if (withdraw(&account1, amount)) { /* Pass by pointer */ printf("Withdrew $%.2f from Account 1.\n", amount); } else { printf("Insufficient funds.\n"); } break; case 3: if (readDouble("Enter transfer amount: $", &amount) != 0) { fprintf(stderr, "Invalid amount.\n"); break; } transfer(&account1, &account2, amount); /* Pass by pointers */ break; case 4: printf("\n--- Account Details ---\n"); printAccount(&account1); /* Pass by pointer (read-only) */ printAccount(&account2); printf("\n"); break; case 5: printf("Exiting.\n"); return EXIT_SUCCESS; default: printf("Invalid choice. Please try again.\n"); break; } } return EXIT_SUCCESS; } /* Initializes account using pass by pointer (modifies account) */ void initializeAccount(Account *acc, const char *owner, int accountNumber, double initialDeposit) { strncpy(acc->owner, owner, MAX_NAME_LEN - 1); acc->owner[MAX_NAME_LEN - 1] = '\0'; acc->accountNumber = accountNumber; acc->balance = initialDeposit; } /* Deposits money using pass by pointer (modifies account) */ void deposit(Account *acc, double amount) { if (amount > 0) { acc->balance += amount; } } /* Withdraws money using pass by pointer (modifies account) */ /* Returns 1 on success, 0 on failure */ int withdraw(Account *acc, double amount) { if (amount > 0 && acc->balance >= amount) { acc->balance -= amount; return 1; } return 0; } /* Gets balance using pass by pointer (read-only, const) */ double getBalance(const Account *acc) { return acc->balance; } /* Transfers money between accounts using pass by pointers */ void transfer(Account *from, Account *to, double amount) { if (withdraw(from, amount)) { deposit(to, amount); printf("Transferred $%.2f from %s to %s.\n", amount, from->owner, to->owner); } else { printf("Transfer failed: insufficient funds in %s's account.\n", from->owner); } } /* Prints account details using pass by pointer (read-only) */ void printAccount(const Account *acc) { printf("Account %d: %s, Balance: $%.2f\n", acc->accountNumber, acc->owner, acc->balance); } /* Demonstrates pass by value (original not modified) */ void demonstratePassByValue(int x) { x = 1000; printf("Inside function: x = %d\n", x); } /* Reads a double safely */ int readDouble(const char *prompt, double *outValue) { char buffer[MAX_LINE_LEN]; char *endPtr; double val; printf("%s", prompt); fflush(stdout); if (fgets(buffer, sizeof(buffer), stdin) == NULL) { return -1; } errno = 0; val = strtod(buffer, &endPtr); if (endPtr == buffer || (*endPtr != '\n' && *endPtr != '\0')) { return -1; } if (errno == ERANGE) { return -1; } *outValue = val; return 0; }

Explanation of Parameter Passing Usage

  1. initializeAccount: Pass by pointer (modifies Account structure).
  2. deposit: Pass by pointer (modifies balance).
  3. withdraw: Pass by pointer (modifies balance, returns status).
  4. getBalance: Pass by const pointer (read-only access).
  5. transfer: Pass by pointers to two accounts (modifies both).
  6. printAccount: Pass by const pointer (read-only).
  7. demonstratePassByValue: Pass by value (original unchanged).
  8. readDouble: Pass by pointer to return multiple values (status + value).

Expected Output (sample run)

text

=== Bank Account Manager === Account 1: Alice Johnson (Balance: $5000.00) Account 2: Bob Smith (Balance: $3000.00) --- Pass by Value Demonstration --- Before function: originalValue = 42 Inside function: x = 1000 After function: originalValue = 42 (unchanged) --- Menu --- 1. Deposit to Account 1 2. Withdraw from Account 1 3. Transfer from Account 1 to Account 2 4. Display Accounts 5. Exit Enter choice: 1 Enter deposit amount: $500 Deposited $500.00 to Account 1. Enter choice: 3 Enter transfer amount: $1000 Transferred $1000.00 from Alice Johnson to Bob Smith. Enter choice: 4 --- Account Details --- Account 1001: Alice Johnson, Balance: $4500.00 Account 1002: Bob Smith, Balance: $4000.00 Enter choice: 5 Exiting.

Common Pitfalls, Edge Cases & Memory Considerations

1. Confusing Pass by Value with Pass by Pointers

Programmers often expect that modifying a parameter passed by value will affect the original. It does not.

c

void doubleValue(int x) { x = x * 2; // Only modifies local copy } int main(void) { int num = 10; doubleValue(num); printf("%d\n", num); // Still 10 return 0; }

Solution: Use pointers if you need to modify the original.

2. Forgetting the Address-of Operator

When passing by pointer, you must pass the address using &.

c

void modify(int *p) { *p = 42; } int main(void) { int x = 10; modify(x); // BUG: passing int, not int* return 0; }

Solution: Always use & when passing variables to pointer parameters: modify(&x);

3. Null Pointer Dereference

Passing a null pointer to a function that dereferences it causes a crash.

c

void printValue(int *p) { printf("%d\n", *p); // Crash if p is NULL } int main(void) { int *ptr = NULL; printValue(ptr); // BUG return 0; }

Solution: Check for NULL before dereferencing: if (p != NULL) { ... }.

4. Passing Large Structures by Value

Copying large structures is inefficient and can cause stack overflow.

c

struct LargeStruct { int data[10000]; // 40 KB }; void process(struct LargeStruct s) { // Copies 40 KB // ... }

Solution: Pass by pointer: void process(const struct LargeStruct *s).

5. Modifying Const Pointers

Attempting to modify through a const pointer causes a compile error.

c

void printData(const int *data) { *data = 42; // Error: cannot modify through const pointer }

Solution: Respect const; use non-const pointers only when modification is intended.

6. Array Decay to Pointer

Arrays decay to pointers when passed to functions; sizeof gives pointer size, not array size.

c

void printSize(int arr[]) { printf("%zu\n", sizeof(arr)); // Pointer size (8 on 64-bit) } int main(void) { int numbers[10]; printf("%zu\n", sizeof(numbers)); // Array size (40) printSize(numbers); return 0; }

Solution: Pass array size as a separate parameter.

7. Returning Multiple Values

C functions can return only one value. To return multiple values, use pointer parameters.

c

void minMax(int arr[], int size, int *min, int *max) { *min = arr[0]; *max = arr[0]; for (int i = 1; i < size; i++) { if (arr[i] < *min) *min = arr[i]; if (arr[i] > *max) *max = arr[i]; } }

8. Dangling Pointers

Returning a pointer to a local variable is undefined behavior.

c

int *createValue(void) { int x = 10; return &x; // BUG: x destroyed on return }

Solution: Use static, allocate with malloc, or pass a buffer.

9. Memory Leaks with Pointer Parameters

If a function allocates memory and the caller doesn’t free it, a leak occurs.

c

void createString(char **str) { *str = malloc(100); strcpy(*str, "Hello"); } int main(void) { char *s; createString(&s); // Missing free(s) return 0; }

Solution: Document ownership; free allocated memory.

10. Aliasing Issues

Two pointers pointing to the same memory can cause unexpected results.

c

void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main(void) { int x = 5; swap(&x, &x); // Swaps with itself; x unchanged printf("%d\n", x); // 5 return 0; }

Solution: Be aware of aliasing; document if functions require non-aliased pointers.


Summary & Best Practices

Parameter passing in C is fundamental to function design. Understanding pass by value and pass by pointers is essential for writing correct, efficient, and safe programs.

Key Takeaways

  • C supports pass by value and pass by pointers.
  • Pass by value copies the argument; original is not modified.
  • Pass by pointers passes the address; original can be modified.
  • Arrays decay to pointers when passed to functions.
  • Structures can be passed by value or pointer; pointer is more efficient for large structures.
  • Use const pointers for read-only access.
  • C does not have true call by reference; pointers achieve similar effects.

Best Practices

  1. Use pass by value for small, read-only data.
  2. Use pass by pointers for large data or when modification is needed.
  3. Use const for pointer parameters when the function should not modify the data.
  4. Check for NULL pointers before dereferencing.
  5. Pass array size explicitly when passing arrays.
  6. Use pointers to return multiple values from a function.
  7. Avoid returning pointers to local variables.
  8. Document ownership of dynamically allocated memory.
  9. Be aware of aliasing; avoid functions that assume non-aliased parameters without documentation.
  10. Enable compiler warnings (-Wall -Wextra) to catch type mismatches in pointer passing.

By mastering parameter passing techniques, you’ll write C functions that are efficient, safe, and maintainable.

Leave a Reply

Your email address will not be published. Required fields are marked *