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
#include <stdio.h>
int main()
{
char arr[5] = { 'a' , 'b' , 'c' , 'd' , 'e' };
printf ( "Before: " );
for ( int i = 0; arr[i] != NULL; i++)
printf ( "%c " , arr[i]);
printf ( "\n" );
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
#include <stdio.h>
int main()
{
char arr[5] = { 'a' , 'b' , 'c' , 'd' , 'e' };
printf ( "Before: " );
for ( int i = 0; arr[i] != '\0' ; i++)
printf ( "%c " , arr[i]);
printf ( "\n" );
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
#include <stdio.h>
int main()
{
char arr[5] = { 'a' , 'b' , 'c' , 'd' , 'e' };
printf ( "Before: " );
for ( int i = 0; arr[i] != '\0' ; i++)
printf ( "%c " , arr[i]);
printf ( "\n" );
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
#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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...