1

I'm stuck at trying to figure out the correct data types for T1 and P1 (see below).

Compiler error:

weather_station_v1_2.ino:76:37: error: invalid operands of types ‘const char*’ and ‘const char [11]’ to binary ‘operator+’

The Code:

if (status != 0) {
      delay(status);
      status = pressure.getPressure(P,T);

      const unsigned char P1 = pressure.getTemperature(P);
      const unsigned char T1 = pressure.getTemperature(T);

      byte data = "temp" + T1 + "&pressure=" + P1;
      httppost();
      delay(1000);
    }

At the end data will be send to a PHP server, but it has to follow the scheme name1=value1&name2=value2.

Would you be so nice and give me a hint about what I'm doing wrong? Thanks

2
  • 1
    are you in need of snprintf()? Commented Jun 22, 2016 at 18:14
  • 1
    yes, it seems so xD Commented Jun 22, 2016 at 18:40

1 Answer 1

2

Your problem is not one of data type conversion, but that in C the operator + is defined only for arithmetic operations (adding numbers, or adding an offset to a pointer).

You're attempting to concatenate strings. There is no "string" type built into C. C "strings" are char arrays, usually terminated with a 0 byte. Arrays in C are fixed size and fixed allocation so there is no array "concatenation" operator in C.

Operations on arrays, and thereby also on C "strings", are usually implemented through manipulation functions. In your particular case the usual approach is the use of a string formatting function of the printf family. You should look at snprintf.

Sign up to request clarification or add additional context in comments.

9 Comments

Nice "hint" answer per OP request. UV (Quibble: Disagree with ""strings ... usually terminated with a 0 byte". In C, strings always end with a null character, else it is not a string.)
Thanks for your help, my code does work now :) if (status != 0) { delay(status); status = pressure.getPressure(P,T); const unsigned char P1 = pressure.getTemperature(P); const unsigned char T1 = pressure.getTemperature(T); char buffer [100]; byte data = snprintf(buffer, 100, "temp=%T1&pressure=%P1", T1, P1); httppost(); delay(1000); }
@hehxes "temp=%T1&pressure=%P1" is an unusual format. Sure it works?
@chux: Yes, the C standard defines that const string literals that are not initializers always terminate with a 0 byte. However this char hello[5] = "hello"; is perfectly legal, but will not terminate with a 0 byte. As far as the standard library str… functions is concerned this is not a string of course. The borders of what a string is and what not are a bit blurry. IMHO the use of the in-band signalling 0 byte terminator was one of the worse decisions in the C design process. Sure, having to drag around the length explicitly is annoying, but it would have prevented so many security bugs.
"hello" is a string literal, not necessarily a string. True C has blurry terminology.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.