Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Pointers and Memory Allocation in C

Introduction to Pointers

Pointers are a fundamental concept in C programming. They are variables that store the memory address of another variable. Unlike regular variables that store data values, pointers store memory addresses.

Declaring Pointers

To declare a pointer, you use the asterisk (*) symbol before the pointer's name. The syntax is:

int *ptr;

This declares a pointer ptr that can point to an integer variable.

Initializing Pointers

You can initialize a pointer by assigning it the address of another variable using the address-of operator (&).

int var = 10;
int *ptr = &var;

Here, ptr now holds the memory address of var.

Dereferencing Pointers

Dereferencing a pointer means accessing the value stored at the memory address the pointer holds. You use the asterisk (*) operator to dereference a pointer.

int var = 10;
int *ptr = &var;
int value = *ptr; // value now holds 10

Pointer Arithmetic

Pointers can be incremented or decremented to point to the next or previous memory location. Pointer arithmetic is particularly useful when working with arrays.

int arr[3] = {10, 20, 30};
int *ptr = arr;
ptr++; // Now ptr points to arr[1]

Dynamic Memory Allocation

Dynamic memory allocation allows you to allocate memory during runtime using functions like malloc, calloc, realloc, and free.

Using malloc

The malloc function allocates a specified number of bytes and returns a pointer to the first byte of the allocated memory.

int *ptr = (int *)malloc(sizeof(int) * 5);

This allocates memory for an array of 5 integers.

Using calloc

The calloc function allocates memory for an array of elements, initializes them to zero, and then returns a pointer to the memory.

int *ptr = (int *)calloc(5, sizeof(int));

This allocates and initializes memory for an array of 5 integers.

Using realloc

The realloc function resizes a previously allocated memory block.

ptr = (int *)realloc(ptr, sizeof(int) * 10);

This resizes the memory block to hold 10 integers.

Freeing Memory

The free function deallocates a previously allocated memory block, making it available for future allocations.

free(ptr);

Example Program

Below is a complete example that demonstrates the use of pointers and dynamic memory allocation.

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

int main() {
    int *ptr;
    int n, i;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    ptr = (int *)malloc(n * sizeof(int));
    if (ptr == NULL) {
        printf("Memory not allocated.\n");
        exit(0);
    }
    printf("Memory successfully allocated using malloc.\n");

    for (i = 0; i < n; ++i) {
        ptr[i] = i + 1;
    }
    printf("The elements of the array are: ");
    for (i = 0; i < n; ++i) {
        printf("%d ", ptr[i]);
    }
    free(ptr);
    printf("\nMemory successfully freed.\n");
    return 0;
}

Output:

Enter number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1 2 3 4 5
Memory successfully freed.