Problems using atoi

MacOS
Arduino IDE2.2.1
Arduino Uno WiFi rev 2
WiFiNINA 1.8.14
Arduino Motor Shield rev 3

for (int i = 0; i < STD_BUFFER_SIZE; ++i) {
  if (incomingData1[i].length() > 0) { // Check if the string is not empty
    localData1 = atoi(incomingData1[i].c_str());
    break; // Stop after converting the first non-empty string
  } //if (incomingData1[i].length() > 0)
} //for (int i = 0; i < STD_BUFFER_SIZE; ++i)

I'm using atoi() to change the incoming TCP/IP string to an int. When I run this code as part of a separate sketch, it compiles fine. When I compile the same code as part of a much larger sketch, I get;

error: request for member 'length' in 'incomingData1.arduino::String::operator[](((unsigned int)i))', which is of non-class type 'char'
if (incomingData1[i].length() > 0) { // Check if the string is not empty

Should I be doing the conversion in a different way?

You shouldn't be using Strings with atoi(). To check the length of a C-string (zero terminated character array), use strlen().

Looks like you've confused String, a class that represents char arrays and the char arrays themselves. Is your buffer a char array or an array of Strings? It looks like you've got bits of code for both things mashed together and it's not going to work.

Try to avoid String when you can on embedded systems. It's there as a crutch for new programmers who haven't learned about arrays yet. But you don't really get to do anything cool until you learn arrays so I say go ahead and learn them.

I spent all day converting to strings. I'll go back to an earlier array-based version.

There's more to learn I guess. My son the programmer says that if you know all about C++, you're wrong.

Thanks!

take the character at position "i" of the variable "incomingData1" and check how long = how many bytes long this single character is.

This makes no sense at all.

You are using the Arduino Uno WiFi rev 2
I don't know the details of the core-files of this arduino-board.

on ESP8266 / ESP32 you have a data-type IPAddress which is in fact an array of bytes where each byte holds one part of the IP-adress

So in fact you can't go wrong.
Never.
Because it would take you all time until the end of the universe to know "everything about C++"

best regards Stefan

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