Using Null Pointer in C Programming

CCBeginner
Practice Now

Introduction

A null pointer is a pointer that does not point to any memory address. In C programming, a null pointer is represented by the constant NULL, which is defined in the header file stdio.h. Using a null pointer can help to avoid errors and add functionality to C programs.

In this lab, you will learn about null pointers and how to use them in C programming. You will create a program that uses null pointers to search for names in an array.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/CompoundTypesGroup(["`Compound Types`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/ControlFlowGroup -.-> c/while_loop("`While Loop`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/ControlFlowGroup -.-> c/break_continue("`Break/Continue`") c/CompoundTypesGroup -.-> c/arrays("`Arrays`") c/CompoundTypesGroup -.-> c/strings("`Strings`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/comments -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/variables -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/data_types -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/operators -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/if_else -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/while_loop -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/for_loop -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/break_continue -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/arrays -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/strings -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/user_input -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/pointers -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/function_parameters -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} c/function_declaration -.-> lab-123293{{"`Using Null Pointer in C Programming`"}} end

Declare a Null Pointer

In C programming, a null pointer is a pointer that points to nothing. To declare a null pointer, you can assign the value NULL to a pointer variable.

#include <stdio.h>

int main() {
   int *ptr = NULL;    // ptr is a NULL pointer
   return 0;
}

Declare a Void Pointer

A void pointer is a pointer that has no specific type. It can be used to hold any type of data. To declare a void pointer, use the keyword void before the asterisk (*).

#include <stdio.h>

int main() {
   void *ptr;    // ptr is a void pointer
   return 0;
}

Use Null Pointer to Mark End of Pointer Array

You can use a null pointer to mark the end of a pointer array in C programming. This can be useful when searching for names or other data in the array.

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

int search(char *ptr[], char* name);

char *names[] = {
    "John",
    "Peter",
    "Thor",
    "Chris",
    "Tony",
    NULL
};

int main(void)
{
    if(search(names, "Peter") != 1)
    {
        printf("Peter is in the list.\n");
    }

    if(search(names, "Scarlett") == -1)
    {
        printf("Scarlett not found.\n");
    }

    return 0;
}

// define the search function
int search(char *ptr[], char*name)
{
    for(int i=0; ptr[i]; ++i)
    {
        if(!strcmp(ptr[i], name))
        {
            return i;
        }
    }

    return -1;  // name not found
}

Customize the Program for User Input

You can customize the program to allow the user to input names for the array and search for names. This can be done using the scanf() function in C programming.

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

#define MAX_NAMES 100

int search(char *ptr[], char* name);

int main(void)
{
    char *names[MAX_NAMES];
    char name[50];
    int count = 0;

    printf("Enter names (press enter to stop):\n");

    // get names from user input
    while(1)
    {
        scanf("%s", name);

        if(strcmp(name, "") == 0)
        {
            break;
        }

        names[count] = (char*) malloc(strlen(name)+1);
        strcpy(names[count], name);
        count++;
    }

    names[count] = NULL;    // mark end of array with null pointer

    // search for names
    while(1)
    {
        printf("Enter name to search for (press enter to stop):\n");
        scanf("%s", name);

        if(strcmp(name, "") == 0)
        {
            break;
        }

        int index = search(names, name);

        if(index != -1)
        {
            printf("%s is found in index %d.\n", name, index);
        }
        else
        {
            printf("%s not found.\n", name);
        }
    }

    return 0;
}

// define the search function
int search(char *ptr[], char*name)
{
    for(int i=0; ptr[i]; ++i)
    {
        if(!strcmp(ptr[i], name))
        {
            return i;
        }
    }

    return -1;  // name not found
}

Summary

In this lab, you learned about null pointers and how to use them in C programming. You saw how to declare a null pointer and a void pointer, and how to use a null pointer to mark the end of a pointer array. You also learned how to customize a program to allow user input and search for names using null pointers. By using null pointers in your programs, you can make them more robust and avoid common errors.

Other C Tutorials you may like