What is the best way to clear a char array in C / C ++?

I was looking for ways to free char array in C / C ++. I came up with this code:

char testName[20]; for(int i = 0; i < sizeof(testName); ++i) { testName[i] = (char)0; } 

It works for a while, but when I try strlen , the result is always two more than the typed word. For example, I enter the word dog , the output will be five. Why is this so? Is my char array not cleared?

Here is my code:

 char testName[20]; void loop() { if(Serial.available()) { Serial.println("Waiting for name..."); index = 0; for(int i = 0; i < sizeof(testName); ++i) { testName[i] = (char)0; } while(Serial.available()) { char character = Serial.read(); testName[index] = character; index++; } Serial.print("Name received: "); Serial.println(testName); Serial.print("The sentence entered is "); Serial.print(strlen(testName)); Serial.println(" long"); delay(1000); } delay(1000); } 

Screenshot:

Exit screenshot

Text output:

 Name received: dog The sentence entered is 5 characters long 
+5
source share
3 answers

Do not use C-shaped arrays in modern C ++. When you need a fixed-size array, use std::array instead. In terms of memory, they are identical.

Then you can clear the array with: myarray.fill('\0')

+13
source

If your definition of empty char array sets all elements of the array to zero, you can use std::memset .

This will allow you to write this instead of your clear loop:

 const size_t arraySize = 20; // Avoid magic numbers! char testName[arraySize]; memset(&(testName[0]), 0, arraySize); 

Regarding the "strange" results of strlen() :

strlen(str) returns "(...) the number of characters in the character array that str points to the first element up to the first zero character. This means that it will count the characters until it finds zero.

Check the contents of the lines you pass to strlen() - you may have white characters (e.g. \r or \n , for example) that were read from the input stream.

Also, tip - consider std::string instead of regular char arrays.


A memset() note: memset() often optimized for high performance. If this is not your requirement, you can also use std::fill , which is a more C ++-like way to populate an array of everything:

 char testName[arraySize]; std::fill(std::begin(testName), std::end(testName), '\0'); 

std::begin() (and std::end ) works well with compile-time arrays.


Also, to respond to @SergeyA's comment, I suggest reading this SO post and this answer .

+4
source

Another quick way to fill an array with zeros is only during initialization:

 char testName[20] = {}; 

same as

 char testName[20] = {0}; 

Since this is a C99 function, the compiler may complain about the following

 warning: ISO C forbids empty initializer braces 

More details here .

In any case, looking at the OP code, there is no need to initialize / populate the array, it might be better:

 #define MAX_LENGTH 20 char testName[MAX_LENGTH]; void loop() { if(Serial.available()) { Serial.println("Waiting for name..."); index = 0; while(Serial.available()) { if(index < MAX_LENGTH) testName[index++] = Serial.read(); } if(index < MAX_LENGTH) { testName[index] = 0; Serial.print("Name received: "); Serial.println(testName); Serial.print("The sentence entered is "); Serial.print(strlen(testName)); Serial.println(" long"); } else Serial.print("Name too long ..."); delay(1000); } delay(1000); } 
+3
source

Source: https://habr.com/ru/post/1275268/


All Articles