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

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