Hello everyone
I am trying to write a function, which will take a byte array as an input and do something with it:
void Render(byte *bytes)
{
while(*bytes)
{
LcdD(*bytes++);
}
}
but unfortunately it isnt working right at all.
I also have a similiar function which does work, but using a char array:
void LcdString(char *characters)
{
while (*characters)
{
LcdCharacter(*characters++);
}
}
Has anybody got an idea what I am doing wrong?
sincerely
bluebubble
system
February 24, 2013, 1:18pm
2
Has anybody got an idea what I am doing wrong?
Without seeing how you call the function? No.
A string is a NULL terminated array of chars. I'm guessing that you have a string that you use to call the second function, and expect that something magic is happening, and that magic will somehow happen in the first function, too.
It will only if the array passed to the first function is also NULL terminated.
If your byte array happens to be e.g. 10 bytes long and you start with an array of all zero's no element will be updated as *byte == 0 == false!
void Render(byte *bytes)
{
while(*bytes)
{
LcdD(*bytes++);
}
}
This can be solved in three ways:
you have a pointer to the last element
you have an additional anchor element in the array with a value that does not occur in the rest
you know the size of the array and use a for loop
void Render1(byte *bytes, byte *last)
{
while(bytes != last)
{
LcdD(*bytes++);
}
}
void Render2(byte *bytes)
{
while(*bytes != 255) // 255 is anchor value
{
LcdD(*bytes++);
}
}
void Render3(byte *bytes, uint8_t size)
{
for (uint8_t i = 0; i< size; i++)
{
LcdD(bytes[i]++);
}
}
Thank you robtillaart
I didnt know the while-function waits for a zero to appear in the array.