Clear Char Array in C
-
Use the
memset
Function to Clear Char Array in C -
Use
bzero
orexplicit_bzero
Functions to Clear Char Array in C
This article will explain several methods of how to clear char array in C.
Use the memset
Function to Clear Char Array in C
The memset
function is generally used to set the memory region with the constant value. The function is part of the standard library and is defined in the <string.h>
header file.
memset
takes three arguments - the first is the void
pointer to the memory region, the second argument is the constant byte value, and the last one denotes the number of bytes to be filled at the given memory address. Note that we can pass 0
integer value to clear the char
array.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void printCharArray(char *arr, size_t len)
{
printf("arr: ");
for (size_t i = 0; i < len; ++i) {
printf("%c, ", arr[i]);
}
printf("\n");
}
#define LENGTH 20
int main(){
char c_arr[LENGTH] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
printCharArray(c_arr, LENGTH);
memset(c_arr, 0, LENGTH);
printCharArray(c_arr, LENGTH);
exit(EXIT_SUCCESS);
}
Output:
arr: a, b, c, d, e, f, g, , , , , , , , , , , , , ,
arr: , , , , , , , , , , , , , , , , , , , ,
Alternatively, memset
can be called with the specific character as the constant byte argument, which can be useful for initializing every given array element with the same values. In this case, we arbitrarily choose the character zero to fill the array, resulting in a cleared memory region.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void printCharArray(char *arr, size_t len)
{
printf("arr: ");
for (size_t i = 0; i < len; ++i) {
printf("%c, ", arr[i]);
}
printf("\n");
}
#define LENGTH 20
int main(){
char c_arr2[LENGTH] = "techarmp array";
printCharArray(c_arr2, LENGTH);
memset(c_arr2, '0', LENGTH);
printCharArray(c_arr2, LENGTH);
exit(EXIT_SUCCESS);
}
Output:
arr: t, e, m, p, , a, r, r, a, y, , , , , , , , , , ,
arr: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Use bzero
or explicit_bzero
Functions to Clear Char Array in C
bzero
is another standard library function to fill the memory area with the zero \0
bytes. It only takes two arguments - the pointer to the memory region and the number of bytes to overwrite. explicit_bzero
, on the other hand, is an alternative that guarantees to conduct the write operation regardless of compiler optimizations. If instructed by the user, the compiler analyzes code for redundant instructions and removes them, and the explicit_bzero
function is designed for this specific scenario.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void printCharArray(char *arr, size_t len)
{
printf("arr: ");
for (size_t i = 0; i < len; ++i) {
printf("%c, ", arr[i]);
}
printf("\n");
}
#define LENGTH 20
int main(){
char c_arr[LENGTH] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
printCharArray(c_arr, LENGTH);
bzero(c_arr, LENGTH);
printCharArray(c_arr, LENGTH);
explicit_bzero(c_arr, LENGTH);
printCharArray(c_arr, LENGTH);
exit(EXIT_SUCCESS);
}