How to clear a string array in C language?
In C language, there are two methods to empty a string array.
Option 1:
Method 1: Assign an empty string to an array using the strcpy function.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
// 清空数组
strcpy(str, "");
printf("%s\n", str); // 输出为空字符串
return 0;
}
Option 1: The second method is to use the memset function to set all elements in the array to 0.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
// 清空数组
memset(str, 0, sizeof(str));
printf("%s\n", str); // 输出为空字符串
return 0;
}
Both methods can be used to empty the string array, the choice between the two ultimately depends on personal preference and project requirements.