. Advertisement .
..3..
. Advertisement .
..4..
One of the core tasks in C programming language is managing memory due to it requires users to interact with the basic structures of memory. Hence, how to clear an array of char in C is also a popular operation that appeared in various scenarios because char arrays are where to store and manipulate string types. In this post, you will be introduced about 2 typical methods for handling char array clearing. Read details below!
How to clear array of char in C
1. Using the memset() function
We can use the memset()
function for setting a region of memory with the unchanged value. This function is contained within the library of standards and identified by the header file <string.h>
.
The memset receives 3 arguments which are:
- str: This is the memory location’s pointer used to set the memory. You can put any memory block type because str is a void pointer. However, the memory would be set in bytes.
- ch: This value is an integer which is will be copied into the block of memory. Before being copied, it will be converted into an unsigned character.
- n: This reflects how many bites in the block of memory are set.
Notably, you can convert zero integer value to empty the array of char.
#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: , , , , , , , , , , , , , , , , , , , ,
Besides that, there is another way to call the memset() function with a certain character, which is an argument of the unchanged byte. Hence, this method will be beneficial and supportive in initializing all given elements of the array with indistinguishable values. In this situation, you can optionally select the character 0 to fill in the array, leading to the consequence of clearing the 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,
2. Using the brezo/explicit brezo functions
bzero is a function of the standard library used for filling the area of the memory with the zero bytes (\0)
. There are 2 arguments taken in bzero: 1-the pointer that points to the region of the memory, 2-the bytes number to overwrite.
Another function to clear an array of char in C is the explicit_bzero
function. Unlike the bzero method, explicit_bzero
will ensure that overwriting the area memory can take place even when the function is deduced to be unnecessary by the compiler optimizations. Keep in mind that this method is an extension without a standard in C so it probably can not be contained in certain compilers.
#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);
}
The Bottom Line
To be concluded, this blog has gone through ways to clear an array of char in C including memset()
and bzero()
or explicit_bzero()
. Hope that the code demonstration above helps you to figure out which method is the easiest for you to perform. However, an interesting fact for you is that bzero()
is regarded as the more underlying algorithm for code treatment. If you are excited about other IT tutorials, explore more posts on this website for more details!
Leave a comment