Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty char array in Arduino

Tags:

c

arduino

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]
like image 870
JAMES Avatar asked Dec 20 '15 13:12

JAMES


People also ask

How do I empty a char array in Arduino?

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.

How do I check if a char array is empty?

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.

How do I empty a char string?

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".

WHAT IS NULL character in Arduino?

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.


1 Answers

Use memset from string.h:

memset(packetBuffer, 0, sizeof packetBuffer);
like image 198
fuz Avatar answered Oct 04 '22 21:10

fuz