Mastering Pointers in C: A Comprehensive Guide

Delve into the world of pointers in C, a powerful tool for memory manipulation and low-level programming. Understand how pointers work, their applications in accessing arrays and dynamic memory, and the best practices for using them safely and effectively.



Applications of Pointers in C

C allows low-level memory access with pointers, which store the address of another variable. Pointers are used for accessing arrays, dynamic memory allocation, and more.

Accessing Array Elements

Pointer variables can access array elements. Incrementing a pointer moves it to the next array element.

Example

#include <stdio.h>

int main(){
    int arr[] = {5, 6, 7, 8, 9};;
    int *ptr = arr;

    for(int i = 0; i < 5; i++){
        printf("arr[%d]: %d\\n", i, *ptr);
        ptr++;
    }
    
    return 0;
}
Output

arr[0]: 5
arr[1]: 6
arr[2]: 7
arr[3]: 8
arr[4]: 9

Dynamic Memory Allocation

Pointers are essential for dynamic memory allocation. Functions like malloc(), calloc(), and realloc() manage memory at runtime.

1. malloc() Function

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

int main(){
    char *name;
    name = (char *) malloc(strlen("NewString") + 1);

    strcpy(name, "NewString");
    
    if(name == NULL) {
        fprintf(stderr, "Error - unable to allocate memory\\n");
    } else {
        printf("Name = %s\\n", name);
    }
}
Output

Name = NewString
2. calloc() Function

int *ptr;
ptr = (int *) calloc(10, sizeof(int));
3. realloc() Function

void *realloc(ptr, newSize);

Passing Arguments by Reference

Passing pointers to functions allows modifying variables in the caller's scope and returning multiple values.

Example

#include <stdio.h>

int swap(int *x, int *y);

int main(){
    int a = 30;
    int b = 40;

    printf("Before swap, a: %d\\n", a);
    printf("Before swap, b: %d\\n", b);

    swap(&a, &b);

    printf("After swap, a: %d\\n", a);
    printf("After swap, b: %d\\n", b);

    return 0;
}

int swap(int *x, int *y){
    int temp = *x;
    *x = *y;
    *y = temp;

    return 0;
}
Output

Before swap, a: 30
Before swap, b: 40
After swap, a: 40
After swap, b: 30

Passing an Array to a Function

Arrays can be passed to functions using pointers, allowing the function to traverse and process the array.

Example

#include <stdio.h>

int max(int *arr, int length);

int main(){
    int arr[] = {12, 45, 23, 89, 6};
    int length = sizeof(arr) / sizeof(int);

    int maxnum = max(arr, length);
    printf("max: %d", maxnum);

    return 0;
}

int max(int *arr, int length){
    int max = *arr;
    
    for (int i = 0; i < length; i++){
        printf("arr[%d]: %d\\n", i, *arr);

        if (*arr > max)
            max = *arr;
        arr++;
    }
    return max;
}
Output

arr[0]: 12
arr[1]: 45
arr[2]: 23
arr[3]: 89
arr[4]: 6
max: 89

Returning Multiple Values from a Function

Use pointers to return multiple values from a function by passing the addresses of variables to be modified.

Example

#include <stdio.h>

void funAddSub(int a, int b, int* add, int* sub) {
  *add = a + b;
  *sub = a - b;
}

int main() {
  int num1 = 15;
  int num2 = 4;
  int res1, res2;

  funAddSub(num1, num2, &res1, &res2);

  printf("Addition is %d and subtraction is %d", res1, res2);

  return 0;
};
Output

Addition is 19 and subtraction is 11