Strstr function on ESP32

I'm trying to use this code...

byte buffer [30] = "happy holidays";
byte *ptr = 0;


void setup() 
{
  Serial.begin(9600);

  ptr = strstr(buffer, "day");
  
  if (ptr)
    Serial.println("found");
  else
    Serial.println("not found");
}

void loop()
{

}

It works as expected on an Uno, but when I tried to upload to an ESP32 board I get the following compilation error.

/var/folders/4z/3g9th3gs4cng3b803dg5bhw40000gn/T/arduino_modified_sketch_211243/Blink.ino: In function 'void setup()':
Blink:9:29: error: invalid conversion from 'byte* {aka unsigned char*}' to 'const char*' [-fpermissive]
   ptr = strstr(buffer, "day");
                             ^
In file included from /Users/paulibbotson/Library/Arduino15/packages/esp32/hardware/esp32/1.0.6/tools/sdk/include/newlib/stdio.h:29:0,
                 from /Users/paulibbotson/Library/Arduino15/packages/esp32/hardware/esp32/1.0.6/cores/esp32/Arduino.h:27,
                 from sketch/Blink.ino.cpp:1:
/Users/paulibbotson/Library/Arduino15/packages/esp32/hardware/esp32/1.0.6/tools/sdk/include/newlib/string.h:40:15: note:   initializing argument 1 of 'char* strstr(const char*, const char*)'
 char  *_EXFUN(strstr,(const char *, const char *));
               ^
/Users/paulibbotson/Library/Arduino15/packages/esp32/hardware/esp32/1.0.6/tools/sdk/include/newlib/_ansi.h:65:30: note: in definition of macro '_EXFUN'
 #define _EXFUN(name, proto)  name proto
                              ^
Blink:9:15: error: invalid conversion from 'char*' to 'byte* {aka unsigned char*}' [-fpermissive]
   ptr = strstr(buffer, "day");
               ^
exit status 1
invalid conversion from 'byte* {aka unsigned char*}' to 'const char*' [-fpermissive]

Looks like it doesn't like the byte format, but just wondering why it works on the Uno.

For strings you need char* and not byte*, though some platforms might support it.

Byte is unsigned char and should be used only for binary data which char anyways supports.

If you must use byte then, explicitly cast byte* to char* while calling the function and cast its return value to byte*

char* inputStr = "hello";
byte* outputStr = (byte*) input; //Might crash

//Better is to first allocate memory and then copy the elements
int strLen = strlen(inoutStr);

outputStr = new byte[len + 1];
memset(outputStr, len + 1, 0);

for (int i = 0; i <= len; i++)
{
    outputStr[i] = (byte)inputStr[i];
}

//Use outputStr

delete[] outputStr; // Clean up

Even better is to know the difference between

char* inputbad = "hello";
char inputgood[] = "hello";

byte* output = (byte*) inputgood; // Won't crash

And always use const where appropriate!

BTW: On the Uno, you get at least the warnings:

warning: invalid conversion from 'byte* {aka unsigned char*}' to 'const char*' [-fpermissive]
warning: invalid conversion from 'char*' to 'byte* {aka unsigned char*}' [-fpermissive]

Thanks... much appreciated

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.