But, as coded, the termination character is inserted whenever the function is called.
How do I avoid this? Can the termination be set only once, somehow when the string is declared?
Is that a problem, that a zero-terminator is written many times at the end ? I don't care, even if you do that 1000 times a second.
The zero-terminator is character zero, you can use '\0', like this : char c = '\0';
Setting the zero-terminator just once seems odd to me, if it is not known how long the string will be.
Suppose that all 10 characters can be filled, in that case the last character should be overwritten with the zero-terminator to make it a valid string.
guy_c:
But, as coded, the termination character is inserted whenever the function is called.
How do I avoid this? Can the termination be set only once, somehow when the string is declared?
If "char string[10];" is a "global variable" within your sketch, you never need to initialize the terminating zero.
All global variable in a sketch are zeroed at the start of the program by default, and if you don't manage to write buggy code with "buffer overflow" writing, the terminating zero in that string keeps there at all times.
But if "char string[10];" is a local variable which is declared within a function, things are different. Then the variable contains garbage from previously used RAM, as it is dynamically created and deleted.
I don't know for what you want to use the variable, but in case that something needs to be zeroed, I always zero out the whole variable and not only the last byte. You can easily use the memset command to zero out the whole contents:
jurs:
If "char string[10];" is a "global variable" within your sketch, you never need to initialize the terminating zero.
All global variable in a sketch are zeroed at the start of the program by default, and if you don't manage to write buggy code with "buffer overflow" writing, the terminating zero in that string keeps there at all times.
What is the text that is received ?
I would not only want to know what text, but also if there is any CarriageReturn and/or LineFeed at the end, which baudrate, how many times a second that is received, which Arduino board you use, what else is connected to the serial port, and so on.
If you declare your variable in a function like the setup() function, the variable IS NOT ZEROED when the functions starts. If the variable is a "local variable" declared in a function, YOU would need to zero the variable or it contains rubbish.
So do it like that:
void setup() {
Serial.begin(9600);
while (!Serial) ;
char string[5];
memset(string,0,sizeof(string)); // zero out the whole string!
for(int i=0;i<4;i++)string='.';
Serial.println(string);
}
Or, other possibility:
void setup() {
Serial.begin(9600);
while (!Serial) ;
char string[5];
for(int i=0;i<4;i++)string='.';
string[4]=0; // put a final zero at the end
Serial.println(string);
}
And put the final zero at the end of the string before printing it.
I tried to simplify the code submitted to the forum and ended up with something wrong.
In practice, my string is a global variable and I noticed that I had a garbage at the end (after the function filled it w/o bothering about termination).
I will try again in the light of this discussion -once i get out of a dipper mud
memset(string,0,sizeof(string)); // zero out the whole string!
The NULL is a stop sign. Why do you need more than one? If a string handling function doesn't stop at the first stop sign, why assume that any more will be useful?
memset(string,0,sizeof(string)); // zero out the whole string!
The NULL is a stop sign. Why do you need more than one? If a string handling function doesn't stop at the first stop sign, why assume that any more will be useful?
That's what I use as a code before I fill the buffer with contents char by char by char.
Like reading chars from Serial into a command buffer.
Or like reading chars from a HTTP request on a webserver.
That way I can break out at any time from a code and have a valid string, without bothering to put a final zero in the string before returning it somewhere.
If I'd have to put a 0 into the buffer after filling it, I'd have to know exactly where to put it. I'd have to decide:
string[x]=0;
string[x-1]=0;
string[x+1]=0;
I'm often too lazy to find out, so instead of putting the correct 0 after filling in the chars, I just zero out the whole char buffer before using it.
8)
That way I can break out at any time from a code and have a valid string, without bothering to put a final zero in the string before returning it somewhere.