Null pointer handling Dry Run in C

Null pointer handling is an interactive C dry run visualizer from the Pointersintro programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Null pointer handling Program Code

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

void safePrint(int *ptr) {
    if(ptr == NULL) {
        printf("Pointer is NULL, cannot dereference!\n");
        return;
    }
    printf("Value: %d\n", *ptr);
}

int main() {
    int *ptr1 = NULL;        // Null pointer
    int *ptr2 = malloc(sizeof(int));  // Allocate memory
    
    if(ptr2 != NULL) {
        *ptr2 = 42;
    }
    
    printf("ptr1: ");
    safePrint(ptr1);
    
    printf("ptr2: ");
    safePrint(ptr2);
    
    // Common null pointer checks
    if(ptr1 == NULL) {
        printf("\nptr1 is NULL - safe to check\n");
    }
    
    // Never dereference NULL pointer
    // int x = *ptr1;  // CRASH! Segmentation fault
    
    free(ptr2);
    ptr2 = NULL;  // Good practice after free
    
    return 0;
}

View the complete Pointersintro programs page.

Program Console Null pointer handling Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.