How to Empty a Char Array in C?
Last Updated :
18 Dec, 2022
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:
- Using the NULL element
- Using strcpy to clear the string
- Using memset to clear
- 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;
}
OutputBefore: 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;
}
OutputBefore: 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;
}
OutputBefore: 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;
}
OutputBefore: a b c
After:
Similar Reads
How to Check if Empty Array in C? Prerequisite: Array in C The array is a type of Data-structure that can store homogeneous data-type elements. The size of the array is fixed. Syntax: int arr[N]; here, the data type of the array is an integer, the name of an array is arr, and the size of the array is N. Example: C // C Program to de
3 min read
How to store words in an array in C? We all know how to store a word or String, how to store characters in an array, etc. This article will help you understand how to store words in an array in C. To store the words, a 2-D char array is required. In this 2-D array, each row will contain a word each. Hence the rows will denote the index
2 min read
Character Array in Java In Java, a character array is a data structure used to store a sequence of characters. The characters are stored in contiguous memory locations and can be accessed by their index, similar to an array of integers or any other data type. Declaring a Character Array A character array can be declared in
5 min read
Length of Array in C The Length of an array in C refers to the maximum number of elements that an array can hold. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all of its elements.In C language, we don't have any pre-defin
3 min read
Pointer vs Array in C Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being:  1. the sizeof operator sizeof(array) returns the amount of memory used by all elements in the array sizeof(pointer) only returns the amount of memory used by the pointer variable itself 2.
1 min read
Maximum Size of an Array in C Array in C is a collection of elements of the same data type that are stored in contiguous memory locations. The size of an array is determined by the number of elements it can store and there is a limit on this size. The maximum size of an array in C is determined by many factors, including the dat
4 min read