3.2 Examples
1. Passing Pointers to Modify Variables in Functions
In C, you can pass pointers to functions to modify the actual values of variables from within the function. This is because you’re passing the memory address of the variable, not a copy of its value.
Example: Swapping Two Numbers Using Pointers
Here, we define a function swap()
that takes pointers to two integers and swaps their values.
#include <stdio.h>
void swap(int *x, int *y) { int temp = *x; // Dereference x to get its value *x = *y; // Dereference y to get its value and assign it to x *y = temp; // Assign temp (old value of x) to y}
int main() { int a = 5, b = 10; printf("Before swap: a = %d, b = %d\n", a, b);
swap(&a, &b); // Passing addresses of a and b to swap
printf("After swap: a = %d, b = %d\n", a, b); return 0;}
Output:
Before swap: a = 5, b = 10After swap: a = 10, b = 5
2. Using Pointers for Dynamic Memory Allocation
You can use pointers to manage dynamic memory allocation inside functions using malloc()
or calloc()
. The allocated memory can then be used in the function.
Example: Dynamically Allocating an Array in a Function
This example demonstrates how to dynamically allocate memory for an array inside a function and return the pointer to the caller.
#include <stdio.h>#include <stdlib.h>
int* create_array(int size) { int *arr = (int*) malloc(size * sizeof(int)); // Allocate memory for the array if (arr == NULL) { printf("Memory allocation failed!\n"); return NULL; }
// Initialize array elements for (int i = 0; i < size; i++) { arr[i] = i * 10; }
return arr; // Return pointer to the array}
int main() { int size = 5; int *arr = create_array(size);
if (arr != NULL) { // Print the dynamically allocated array for (int i = 0; i < size; i++) { printf("arr[%d] = %d\n", i, arr[i]); }
// Don't forget to free the allocated memory free(arr); }
return 0;}
Output:
arr[0] = 0arr[1] = 10arr[2] = 20arr[3] = 30arr[4] = 40
3. Function Pointers
A function pointer is a pointer that points to a function instead of a data variable. This allows you to pass functions as arguments, making it useful for implementing callback functions.
Example: Using Function Pointers to Implement a Simple Calculator
This example uses function pointers to implement a simple calculator that can perform different operations (addition, subtraction, multiplication, and division) based on the function pointer passed to it.
#include <stdio.h>
// Function declarationsint add(int x, int y) { return x + y;}
int subtract(int x, int y) { return x - y;}
int multiply(int x, int y) { return x * y;}
int divide(int x, int y) { if (y != 0) { return x / y; } return 0; // Prevent division by zero}
// Function that takes two integers and a function pointerint calculate(int x, int y, int (*operation)(int, int)) { return operation(x, y);}
int main() { int a = 20, b = 10;
// Define function pointer int (*func_ptr)(int, int);
// Add two numbers using function pointer func_ptr = add; printf("Addition: %d + %d = %d\n", a, b, calculate(a, b, func_ptr));
// Subtract two numbers using function pointer func_ptr = subtract; printf("Subtraction: %d - %d = %d\n", a, b, calculate(a, b, func_ptr));
// Multiply two numbers using function pointer func_ptr = multiply; printf("Multiplication: %d * %d = %d\n", a, b, calculate(a, b, func_ptr));
// Divide two numbers using function pointer func_ptr = divide; printf("Division: %d / %d = %d\n", a, b, calculate(a, b, func_ptr));
return 0;}
Output:
Addition: 20 + 10 = 30Subtraction: 20 - 10 = 10Multiplication: 20 * 10 = 200Division: 20 / 10 = 2
These examples illustrate the versatility and power of pointers in C functions. Let me know if you need more explanations or additional examples!