Byte Array Help Needed

Can someone help me explain how to easily access bytes in a byte array?

void IncomingByte (const byte c)
{
  if (tailindex <= 256)
  {
    ringbuffer[tailindex++] = c; 
    Serial.println (ringbuffer[tailindex]);  //this doesn't work for accessing bytes
  }
  else
  {
    tailindex = 0;
  }
  if (tailindex == 8)
  {
    for (int x = 0; x < 8; x++)
    {
      Serial.println (ringbuffer[x]);    //this works fine for accessing bytes
    }
  }
}

This line seems strange to me

ringbuffer[tailindex++] = c;

I think I would try

tailindex++;
ringbuffer[tailindex] = c;

Can you explain why that line "seems strange"?

ringbuffer[tailindex++] = c;

Nothing wrong with that assuming that the author meant to write it that way. It sets the value of the current entry in the array then increments the index to the array.

Can someone help me explain how to easily access bytes in a byte array?

byte anArray[] = {12, 23, 45};

void setup()
{
  Serial.begin(115200);
  for (int x = 0; x < sizeof(anArray); x++)
  {
    Serial.println(anArray[x]);
  }
}

void loop()
{
}

Hi,
Can someone explain why this cant work, tailindex is and int and has a value.

ringbuffer[tailindex++] = c; 
Serial.println (ringbuffer[tailindex]);  //this doesn't work for accessing bytes
ringbuffer[tailindex++] = c; 
Serial.println (ringbuffer[tailindex]);  //this doesn't work for accessing the byte just saved because tailindex has been incremented since then

What do you mean "can't work" ?

tailindex = 0;

ringbuffer[tailindex++] = c;               //now ringbuffer[0] has the byte read from c  byte "201"
Serial.println (ringbuffer[tailindex]);   //this just prints a 0
Serial.println (ringbuffer[0]);              //this prints  the correct byte "201"

You incremented tailindex - what did you expect?

ringbuffer[tailindex++] = c;                     //now ringbuffer[0] has the byte read from c  byte "201"
Serial.println (ringbuffer[tailindex - 1]);    //maybe this can work?

"maybe" ?

It would have been quicker to try it than to post it.

Thanks for your help, I will try it out when I get my hands back on my hardware.

ringbuffer[tailindex++] = c;                     //now ringbuffer[0] has the byte read from c  byte "201"
Serial.println (ringbuffer[tailindex - 1]);    //maybe this can work?

ringbuffer[tailindex] = c;                        //now ringbuffer[0] has the byte read from c  byte "201"
Serial.println (ringbuffer[tailindex]);        //Trying this too,
tailindex++;                                          //Trying this too, your awesome me not so much
Serial.println(ringbuffer[tailindex++] = c);

Or the rather obvious:

ringbuffer[tailindex++] = c;                     //now ringbuffer[0] has the byte read from c  byte "201"
Serial.println (c);

You know whats in the buffer, you just put it there, just print the value you put in.

ring buffers of size 256 are particularly easy to fill/empty IFF the index is uint8_t.
if index is NOT uint8_t , or the buffer is NOT size 256, you have do more work; it might be a LOT more, percentagewise, but it's still 'not very much'.

I'm curious why you are passing something that is not a string to a function that expects a string, but still expecting the function to operate properly.

How can you expect to make ringbuffer into a string?

Why the hell aren't you using camelCase for your names?

SamIAm93:
This line seems strange to me

ringbuffer[tailindex++] = c;

I think I would try

tailindex++;
ringbuffer[tailindex] = c;

and what happens when tailindex = the last element of the array? I don't understand "trying" some code. When you put code in a sketch, you put it there because you know what t is going to do.

you put it there because you know what t is going to do.

Or, you think you know what it is going to do. I'm surprised every day when my code doesn't do what I expect. Then, I fix it.