How return a sprintf variable from a function

I want to use a function to perform the following:

char getRTCtime(){
   M5.RTC.getTime(&RTCtime);
  sprintf(timeStrbuff, "%02d:%02d:%02d", RTCtime.hour, RTCtime.min, RTCtime.sec);
  return timeStrbuff;
}

But if I define the function as char, as in the example and call the function with

timeStrbuff = getRTCtime();

I get the error invalid conversion from 'char' to 'char*' [-fpermissive]

If I label the function as a String I get an incompatible error. How can I use a funtion to pass the timeStrbuff variable?

Your snippet of code makes it look like timeStrbuff is a global variable. If so then you don't need to return it from the function

Thanks. Brain fog! I'll leave the rest of the coding till tomorrow.

You do need to be careful when returning a char* from a function. If timeStrbuff had been declared as a local variable inside the function, then it will cease to exist when the function exits (unless it is declared as static).

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