Why does this function require "const *char pStr ="? getTextBounds of GFX lib?

I'm experimenting and learning how to work with the TFT screen.

I'm trying to learn how to work with custom fonts (FreeSans12pt7b for example) and I'm working on a function that helps with centering and placing text.

It makes sense to call my "getTextParameters" function on whatever string I have in mind which works great... until I need to do it to a 2nd string. Then it, of course, doesn't like me trying to change the pStr.

For anyone unfamiliar the getTextBound info is included below:

tft.getTextBounds(string, x, y, &x1, &y1, &w, &h);
getTextBounds expects a string, a starting cursor X&Y position (the current cursor position will not be altered), and addresses of two signed and two unsigned 16-bit integers. These last four values will then contain the upper-left corner and the width & height of the area covered by this text — these can then be passed directly as arguments to fillRect().

  int16_t    nCurX = 0;
  int16_t    nCurY = 0; //just need some base coordinates for measuring these don't matter
  int16_t    nTxtX, nTxtY;
  uint16_t   nTxtSzW, nTxtSzH;
  tft.getTextBounds(pStr, nCurX, nCurY, &nTxtX, &nTxtY, &nTxtSzW, &nTxtSzH);

I have finally got some code that doesn't throw any complaints... but it does use String and I've read i shouldn't be using Strings.

const char *mStr = "TEST";


void textParams(String mStr, uint16_t &mCenter, uint16_t &mWidth, uint16_t &mHeight) { //pass this function a string and it will tell you where to set cursor (sCenter) to center it
  const String pStr = mStr;
  int16_t    nCurX = 0;
  int16_t    nCurY = 0; //just need some base coordinates for measuring these don't matter
  int16_t    nTxtX, nTxtY;
  uint16_t   nTxtSzW, nTxtSzH;
  tft.getTextBounds(pStr, nCurX, nCurY, &nTxtX, &nTxtY, &nTxtSzW, &nTxtSzH);
  mCenter = (tft.width() - nTxtSzW) / 2;
  mWidth = nTxtSzW;
  mHeight = nTxtSzH;
}

Is this correct or should I be using some better practice for getting what I need done, done?

Open to all advice as I'd like to learn the best practices.

pStr is not constant, so perhaps it should not be declared const. Not sure what this declaration does in a function, but it seems like a good place to start.

It is generally considered preferable to use a C string instead of a String, but I think that you are stuck if you are using a library function that takes a String.

Ok... so I'm still inexperienced and not really understanding. :frowning:

Here is what is working:

   String = pStr = "Hello World";
   int16_t    nCurX = 0;
   int16_t    nCurY = 0; //just need some base coordinates for measuring these don't matter
   int16_t    nTxtX, nTxtY;
   uint16_t   nTxtSzW, nTxtSzH;

   tft.getTextBounds(pStr, nCurX, nCurY, &nTxtX, &nTxtY, &nTxtSzW, &nTxtSzH);

   tft.println(nTxtSzW);
   tft.println(nTxtSzH);
   tft.println(pStr);

Also using:

const char* pStr = "Hello World";

Works.

Now,

char* pStr = "Hello World";

works but it does complain:
"warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]"

So what is the correct, "best practice" thing to do?

Should I just use String?

**** the function that is being called is below*****

void Adafruit_GFX::getTextBounds(const char *str, int16_t x, int16_t y,
        int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
    uint8_t c; // Current character

    *x1 = x;
    *y1 = y;
    *w  = *h = 0;

    int16_t minx = _width, miny = _height, maxx = -1, maxy = -1;

    while((c = *str++))
        charBounds(c, &x, &y, &minx, &miny, &maxx, &maxy);

    if(maxx >= minx) {
        *x1 = minx;
        *w  = maxx - minx + 1;
    }
    if(maxy >= miny) {
        *y1 = miny;
        *h  = maxy - miny + 1;
    }
}