I am working on small project using Arduino. I have this char array which used to store some values. the problem is How to set this char array to null after assign some values in Arduino?
char packetBuffer[200]
Start by using '/0' (null) to terminate your string, this is the normal way of dealing with strings. Then in readAtString after the call to processAtString set at_buffer[0]='/0'; and buffidx =0; buffidx should be a byte not a char as its a counter not a character.
So, if you want to see if a char array is storing an empty string, that means checking whether there are 0 characters, instead of 1 or more characters, before the NUL.
There's really no concept of emptying a char string in C. It's simply a pointer that points to some allocated memory. You can reuse that memory in any way you wish, without "emptying" it first. So, the easiest way to empty it is "just don't".
Null termination Generally, strings are terminated with a null character (ASCII code 0). This allows functions (like Serial. print() ) to tell where the end of a string is. Otherwise, they would continue reading subsequent bytes of memory that aren't actually part of the string.
Use memset
from string.h
:
memset(packetBuffer, 0, sizeof packetBuffer);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With