How could you compare two buffer with an offset in starting point ?
void setup() {
Serial.begin(19200);
char Buff1[30] = "abcdefghijklmnopqrst";
char Buff2[30] = "123456abcdefghijklmnopqrst";
if (strcmp(Buff1, Buff2) == 0) {
Serial.println("COMPARE TRUE");
}
else {
Serial.println("COMPARE FALSE");
}
}
// obviously it dont work
is it possible to shift Buff2 comparison 6 time to the right without changing any of the buffer ?
Thanks
Try this
void setup()
{
Serial.begin(115200);
char Buff1[30] = "abcdefghijklmnopqrst";
char Buff2[30] = "123456abcdefghijklmnopqrst";
if (strcmp(Buff1, Buff2 + 6) == 0)
{
Serial.println("COMPARE TRUE");
}
else
{
Serial.println("COMPARE FALSE");
}
}
void loop()
{
}
oh , it’s that simple
any other way that could work ? (for learning more)
like
strcmp(Buff1, Buff2[6])
or with & or * somewhere before variable ?
Thanks
That won't work
The first method works because Buff2 is a pointer to the Buff2 array so adding 6 to it adds 6 to the address and it points to a starting point later in the array. However, Buff2[6] is a single character in the array so the comparison fails
However, this works
void setup()
{
Serial.begin(115200);
char Buff1[30] = "abcdefghijklmnopqrst";
char Buff2[30] = "123456abcdefghijklmnopqrst";
if (strcmp(Buff1, &Buff2[6]) == 0)
{
Serial.println("COMPARE TRUE");
}
else
{
Serial.println("COMPARE FALSE");
}
}
void loop()
{
}
because the ampersand means "address of" rather than "value of"
1 Like
gcjr
October 5, 2025, 11:50am
5
strstr() allows you to locate one string within another. it fails if there is no match
gcjr
October 5, 2025, 2:25pm
6
output
find: def defghijklmnopqrst
find: 248 not found
find: 6abc 6abcdefghijklmnopqrst
char *
find (
const char *haystack,
const char *needle )
{
char *s = strstr (haystack, needle);
Serial.print ("find: ");
Serial.print (needle);
Serial.print (" ");
if (s)
Serial.println (s);
else {
Serial.println ("not found");
}
return s;
}
// -----------------------------------------------------------------------------
void setup ()
{
Serial.begin (19200);
const char *haystack = "123456abcdefghijklmnopqrst";
find (haystack, "def");
find (haystack, "248");
find (haystack, "6abc");
}
// -----------------------------------------------------------------------------
void loop ()
{
}
2 Likes