My first thought is that when I'm having a problem figuring something, I don't show a snippet that omits the context in which the problem is occurring.
If I show a sketch that illustrates the problem happening, others can try it for themselves and see if they see the same issue.
For example:
#include <Wire.h>
#include <Adafruit_SH110X.h>
#define i2c_Address 0x3c
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
const char buf[] = "23rd September";
Serial.begin(115200);
delay(250);
display.begin(i2c_Address, true);
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.clearDisplay();
drawCentreString(buf, 64, 10);
drawCentreString("23rd September", 64, 18);
display.display();
}
void loop() {}
void drawCentreString(const char *buf, int x, int y) {
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h);
display.setCursor(x - w / 2, y);
display.print(buf);
Serial.println(w);
}
Which demonstrates that the text width is calculated as 124 for both string literals and a const char buf containing the same text. Thereby demonstrating that the "when I try to use something that is not a const char" was a wild goose chase.
I then might modify the example and discover that a short string literal centred just fine.
I then might gradually increase the length of the string literal until the problem occurred again. And wonder what was special about 11 characters when that length triggered the problem?
Hmm. Default font width is 6 pixels. So 6 x 11 = 66. getTextBounds was asked to find the text bounds of an 11 character string, beginning printing at x=64, and got confused when the string ended up extending past the right hand side of the screen.
Hmm again. I wonder what would happen if I changed the x & y passed to getTextBounds to 0 and 0? display.getTextBounds(buf, 0, 0, &x1, &y1, &w, &h);
Well, look a that, it centred properly. Now let's try it with the full "23rd September".
Hey that works too. And it works with both a string literal and a const char buffer.
Turns out it was just me using the getTextBounds function incorrectly. It was trying (and failing) to calculate the bounds of a text string that started printing at x=64 that was longer than the remaining number of pixels left (64<66).
But that's just me. YMMV