Open In App

How to Empty a Char Array in C?

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Char Array in C

Char arrays are used for storing multiple character elements in a continuous memory allocated. In a few questions, we have the necessity to empty the char array but You can’t clear a char array by setting the individual members to some value indicating that they don’t contain anything. So, we need some other methods to perform this task. Let us check a few methods to perform this task.

Methods to empty a char Array in C are mentioned below:

  1. Using the NULL element 
  2. Using strcpy to clear the string
  3. Using memset to clear 
  4.  Clearing dynamic char array using free

1.  Clearing String in C using (‘/0’)

The ‘\0’ element in a string or char array is used to identify the last element of the char array. For an empty char array, the first element is the ‘\0’ element. So, we can use this property of string to clear an array.

Example:

C




// C Program to implement
// NULL element property
// to clear the array
#include <stdio.h>
  
int main()
{
    // declaring the array
    char arr[5] = { 'a', 'b', 'c', 'd', 'e' };
  
    printf("Before: ");
  
    for (int i = 0; arr[i] != NULL; i++)
        printf("%c ", arr[i]);
  
    printf("\n");
  
    // Clearing the array
    arr[0] = '\0';
  
    printf("After: ");
  
    for (int i = 0; arr[i] != NULL; i++)
        printf("%c ", arr[i]);
  
    return 0;
}


Output

Before: a b c d e 
After: 

2. Using strcpy to clear the string

strcpy is the predefined function already existing in the string header file. strcpy function helps us to copy elements of one array into another.

Syntax:

strcpy(first,second);

Here first is the element where we need to copy elements and second is the array from which we are copying the elements.

Example:

C




// C Program empty char array 
// Using strcpy to clear 
// the string
#include <stdio.h>
  
int main()
{
    // Creating char array
    char arr[5] = { 'a', 'b', 'c', 'd', 'e' };
  
    printf("Before: ");
  
    for (int i = 0; arr[i] != '\0'; i++)
        printf("%c ", arr[i]);
  
    printf("\n");
  
    // Copying elements of empty array in arr
    strcpy(arr, "");
  
    printf("After: ");
  
    for (int i = 0; arr[i] != NULL; i++)
        printf("%c ", arr[i]);
  
    return 0;
}


Output

Before: a b c d e 
After: 

3. Using memset to Empty string

It copies a single character for a specified number of times to an object. We can empty a char array in C by setting all of the elements to ‘\0’. So, we can change every index element to ‘\0’, as the first element is ‘\0’ so the char array[] will be treated as empty. This work if the array is not dynamic.

Example:

C




// C Program to empty char array
// using memset
#include <stdio.h>
  
int main()
{
  
      // character array
    char arr[5] = { 'a', 'b', 'c', 'd', 'e' };
  
    printf("Before: ");
  
    for (int i = 0; arr[i] != '\0'; i++)
        printf("%c ", arr[i]);
  
    printf("\n");
  
      // using memset
    memset(arr, '\0', sizeof(arr));
  
    printf("After: ");
  
    for (int i = 0; arr[i] != NULL; i++)
        printf("%c ", arr[i]);
  
    return 0;
}


Output

Before: a b c d e 
After: 

4. Using free to empty dynamic char array

If we are using a dynamic char array, means we are allocating space to the array so the simple method to empty is to free the allocated space.

Example:

C




// C program to empty char array using
// free and dynamic array
#include <stdio.h>
  
int main()
{
  
    char* arr;
  
    arr = (char*)malloc(5);
  
    arr[0] = 'a';
    arr[1] = 'b';
    arr[2] = 'c';
  
    printf("Before: ");
  
    for (int i = 0; arr[i] != '\0'; i++)
        printf("%c ", arr[i]);
  
    printf("\n");
  
    free(arr);
  
    printf("After: ");
  
    for (int i = 0; arr[i] != NULL; i++)
        printf("%c ", arr[i]);
  
    return 0;
}


Output

Before: a b c 
After: 


Last Updated : 18 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads