TutorialsCPointers
Share:

Pointers

advanced

Part of Advanced C

Theory

Pointers are variables that store memory addresses. They are one of C's most powerful features, enabling direct memory manipulation, dynamic allocation, and efficient array/function operations.

Declaring a pointer uses the * symbol:

int *ptr;    // pointer to an integer
char *cp;    // pointer to a character
float *fp;   // pointer to a float

The address-of operator & gets the memory address of a variable:

int x = 42;
int *ptr = &x;  // ptr stores the address of x

The dereference operator * accesses the value at the address stored in a pointer:

printf("%d", *ptr);  // prints 42 (the value of x)
*ptr = 100;          // changes x to 100

Pointer arithmetic allows incrementing/decrementing pointers. Adding 1 to an int* advances by sizeof(int) bytes (usually 4).

int arr[] = {10, 20, 30};
int *p = arr;       // points to arr[0]
printf("%d", *p);   // 10
p++;                // now points to arr[1]
printf("%d", *p);   // 20

Pointers and arrays are closely related. An array name is a pointer to its first element. arr[i] is equivalent to *(arr + i).

Passing pointers to functions allows modifying variables in the calling scope (simulating pass-by-reference):

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

Pointers to pointers (int **ptr) store the address of another pointer. Used in multi-dimensional arrays and dynamic arrays of strings.

Dynamic memory allocation uses functions from <stdlib.h>:

  • malloc(size) — allocates memory on the heap, returns void*
  • calloc(count, size) — allocates and zero-initializes
  • realloc(ptr, new_size) — resizes existing allocation
  • free(ptr) — deallocates memory
int *arr = (int*)malloc(5 * sizeof(int));
free(arr);  // always free!

NULL pointer (NULL or 0) points to nothing. Always check for NULL after allocation and before dereferencing.

Function pointers store the address of a function, enabling callback mechanisms:

int (*funcPtr)(int, int) = &add;
int result = funcPtr(3, 5);

Practical Examples

Example 1: Pointers, Addresses, and Swapping
c
Example 2: Dynamic Memory Allocation
c

Exercises

Pointer Basics Practice

easy

Write a program that declares an integer variable, a pointer to it, and demonstrates: printing the variable's address, the pointer's value, and the dereferenced value. Then modify the variable through the pointer.

Starter Code:

#include <stdio.h>\n\nint main() {\n  int num = 42;\n  int *ptr = #\n  // Your code here\n  return 0;\n}

Expected Output:

num = 42\nAddress of num: 0x...\nptr points to: 0x...\nDereferenced: 42\nAfter *ptr = 99: num = 99

Dynamic Array Sum

medium

Write a program that asks the user for the number of elements, dynamically allocates an array using malloc(), fills it with user input, calculates the sum using pointer arithmetic (not array indexing), and frees the memory.

Expected Output:

How many numbers? 5\nEnter 5 numbers: 10 20 30 40 50\nSum: 150

String Copy with Pointers

medium

Write a function 'stringCopy' that copies a source string to a destination string using only pointer arithmetic (no array indexing). Do not use strcpy().

Expected Output:

Hello, Pointers!

Mini Quiz

Mini Quiz

Mini Project

Mini Project: Dynamic Contact Manager

Create a C program that manages a dynamic list of contacts using pointers, dynamic memory allocation, and string operations.

Requirements:

    Bonus Challenge

    Add a search function that takes a pointer to a string and returns a pointer to the matching contact, or NULL if not found.