myBuffer + i + len

char myBuffer[1024];
int i;
int len = strlen(term);


if (strncmp(myBuffer + i - len, term, len) == 0)
...

Can someone tell me what myBuffer + i - len is doing?

The statement is in a function which is reading the serial a byte at a time.
term is declared

char * term

When the string term is read, the function stops reading.

It's called "pointer arithmetic".

It doesn't make sense but it's probably because you didn't post all the code :wink:

MorganS:
It's called "pointer arithmetic".

I surmised as much, but what is myBuffer by itself?

I sometimes wonder why I bother asking.

guix:
It doesn't make sense but it's probably because you didn't post all the code :wink:

That is the actual line from the code.
As I wrote before, if term string is received, then if statement is false.

I am trying to understand what myBuffer is when used in such a manner.

ieee488:
That is the actual line from the code.
As I wrote before, if term string is received, then if statement is false.

I am trying to understand what myBuffer is when used in such a manner.

An array name when used without any brackets is a pointer to the first element.

As Delta-G pointed out, an array name without the brackets is the memory address where that array starts in memory. So, if the following array were stored at memory address 100:

  • char buffer[10];*

then:

  • myFunction(buffer); // Sends lvalue 100 (i.e., a memory address) to myFunction()*
  • myFunction(&buffer[0]); // Send the same lvalue to myFunction()*

Run the following code and see if you can figure things out:

void setup() {

  char myBuffer[] = {"0123456789####456789"};
  char term[] = "####";
  int i;
  int len = strlen(term);

  Serial.begin(9600);

  for (i = 0; i < sizeof(myBuffer); i++) {

    if (strncmp(myBuffer + i - len, term, len) == 0) {
      Serial.print("---> Matched when i = ");
      Serial.println(i);
    } else {
      Serial.println("No match");
    }
  }
}

void loop() {

}

It can also be written like this...

 &myBuffer[i - len]