hi, i want to know how can I empty this array using memset function.
is empty an array means filling it with zeros please confirm.
void setup() {
Serial.begin(115200);
char str[18] = "My name is Arduino";
Serial.println(str);
hi, i want to know how can I empty this array using memset function.
is empty an array means filling it with zeros please confirm.
void setup() {
Serial.begin(115200);
char str[18] = "My name is Arduino";
Serial.println(str);
You can empty string, that is what your array represents, by making first element 0. you can free memory of dynamically allocated array, but static arrays always contain something
Also the example you give won’t compile as you try to assign the string that is longer than array
Your string is not big enough to hold the value (18 characters) you are trying to initialise it with. You need a minimum of 18 + 1 (for the null terminator).
As @killzone_kid mentioned, to clear the string (for printing) simply put the null terminator at the start of the array.
str[0] = '\0';
Serial.println(str);
If you really want every character to contain 0x00, then
memset(str, '\0', 19);
but make str bigger first.
I see in the first post 18 chars for the cString memory allocation and then 19 for the memset. So there is a mismatch.
If you don’t want to risk missing changes or miscalculating the size in the declaration of the cString then use
char str[] = "My name is Arduino";
And then
memset(str, '\0', sizeof str);
The compiler will calculate the necessary size for you
That won’t release the memory though. As this is a local variable, memory will be freed up at the end of the setup() function
What do mean by "emptying an array"? When we take out everything from a bucket, it is empty (there is nothing). The array is composed of a number of memory locations; they must contain some values which are 00 or non-zero. How can these locations be emptied?
I am asking if I have defined an array and now I want to empty it using memset function how will I do
Did you read post #3?
@kashifjaved, please don't modify a post after people have commented on it. In future, rather post an updated code in a new post.