Go Down

Topic: use a byte array in a function (Read 751 times) previous topic - next topic

bluebubble

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


PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

robtillaart

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!
Code: [Select]

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

Code: [Select]

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]++);
  }
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

bluebubble

Thank you robtillaart

I didnt know the while-function waits for a zero to appear in the array.


Go Up