Warning: cast to pointer from integer of different size

Here is part of a sketch and on line 142 I get a warning of a cast from integer to char*. Two lines up, on line 140 I have the same code and get no warning. Both are uint8_t integers cast to a char*. Mystery!

Code:

    133 sensors.requestTemperatures();
134  uint8_t tempC;
135  for (uint8_t i = 0; i < sensors.getDeviceCount(); i++)
136  {
137    tempC = sensors.getTempC(DevAddr[i]);         // float > int
138    oledFill(&ssoled, 0, 1);
139    oledWriteString(&ssoled, 0,  0,   0,(char *)"Nr ", FONT_SMALL, 0, 1);
140    oledWriteString(&ssoled, 0, -1,  -1,(char *)(i + 1), FONT_SMALL, 0, 1);
141    oledWriteString(&ssoled, 0, -1,  -1,(char *)": ", FONT_SMALL, 0, 1);
142    oledWriteString(&ssoled, 0, -1,  -1,(char *)(tempC), FONT_SMALL, 0, 1);
143    oledWriteString(&ssoled, 0, -1, -10,(char *)" C", FONT_SMALL, 0, 1);
144    oledWriteString(&ssoled, 0, -1, -10,(char *)223, FONT_SMALL, 0, 1);
145  }

Compiler output:

    Compiling .pio\build\esp32dev\src\main.cpp.o
src\main.cpp: In function 'void loop()':
src\main.cpp:142:54: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
     oledWriteString(&ssoled, 0, -1, -1,(char *)(tempC), FONT_SMALL, 0, 1);
                                                      ^
Retrieving maximum program size .pio\build\esp32dev\firmware.elf 

ss_oled.cpp:

int oledWriteString(SSOLED *pOLED, int iScroll, int x, int y, char *szMsg, int iSize, int bInvert, int bRender)    indent preformatted text by 4 spaces

You cannot cast a uint8_t into a char* and expect it to write out the string representation of it. What you’re coding is that the number itself shall be interpreted as a pointer into memory – so if you have tempC = 23, it will try to go to memory address 23 and try and print a string from there (that is, print all bytes until a 0x00 byte is found). That’s not what you want, since there’s probably garbage there and not the string representation of 23.

You’ll have to convert it to a string first, e.g. using a buffer and the itoa function (interger-to-ascii).

char printBuf[8];
sensors.requestTemperatures();
uint8_t tempC;
for (uint8_t i = 0; i < sensors.getDeviceCount(); i++)
{
    tempC = sensors.getTempC(DevAddr[i]);         // float > int
    oledFill(&ssoled, 0, 1);
    oledWriteString(&ssoled, 0,  0,   0,(char *)"Nr ", FONT_SMALL, 0, 1);
    itoa(i+1, printBuf);
    oledWriteString(&ssoled, 0, -1,  -1,printBuf, FONT_SMALL, 0, 1);
    oledWriteString(&ssoled, 0, -1,  -1,(char *)": ", FONT_SMALL, 0, 1);
    itoa(tempC, printBuf);
    oledWriteString(&ssoled, 0, -1,  -1, printBuf, FONT_SMALL, 0, 1);
    oledWriteString(&ssoled, 0, -1, -10,(char *)" C", FONT_SMALL, 0, 1);
    //oledWriteString(&ssoled, 0, -1, -10,(char *)223, FONT_SMALL, 0, 1); /* this is also invalid.*/
 }

I’m not sure what you wanted to do in the last line there… printing the number 223?

Also this function should be const-correct – with const char* szMsg, since it probably doesn’t have to modify the message. That would also allow you to drop all the (char*) casts in front of string constants.

maxgerhardt
I’ve sorted out all the issues with casting/converting but come across a problem I see that others have had. First I suddenly got error 2 all the time when uploading and tried a few things to no avail. Then I found this info on GitHub where it says that GPIO2 should be left unconnected. Well, that was the pin I had picked for the OneWire sensor DS18B20. I changed to GPIO14 and uploading worked again (magic). I still couldn’t detect the sensor on the bus so I continued my search and found someone who advised calling “sensors.begin()” twice so I tried that and, lo and behold, it found the sensor. Could you, or anyone else for that matter, explain this odd behaviour.

I have been trying for half an hour to convert integer to ascii using itoa.
I’ve tried various combinations and I can’t understand what I’m doing wrong.

int symbVal = 223;
char symbChar[1];
itoa(symbVal, symbChar, 10);

char *itoa(int val, char *s, int radix)
array subscript is above array bounds [-Warray-bounds]
View Problem (Alt+F8)
No quick fixes available

A character buffer of size 1 cannot hold the string “223”, which would be the conversion result of the number 223. Mind the zero-terminator at the end of C string. So, this should be char symbChar[4].