Going crazy with strings...

Hello Forum, I am pretty new in the world of arduino, and I got stuck in a project. I am trying to send a command to a OBD2-Interface (Onboard Diagnostic Bus of my car), what works fine. Also processing the answer is ok. But it seems I'm too stupid to make it beautiful. My Prob:

I declare a strinf of chars:

char obddesc[12];

and later in a function I try to fill this string:

int getobd(int var)
{
 switch (var) {
   case 1:
     getstuff("04");  // request command "4" from obd2
     obdunit[0] ='%'; // the unit of the result is percent
     obddesc[0] = 'EngLd'; // description is "engine load"
     result = ((a*100)/255);
     break;
   case 2:
..
..

The Problem is, that in the main loop I can not print "obddesc":

   Serial.println(obddesc);

It just appears a "d", the last character of "EngLd". How can I make it work proper, to that the whole word is in my string?

Thanks in advance!

This…

'EngLd'

…is a character constant. It defines a single character (the letter dee). This…

"EngLd"

…is a string constant. It defines a string of characters. (which is what you want) The difference is the quote: single quotes for character constants; double quotes for string constants.

It isn’t necessary to build the string to print. You can call print and println for each piece…

   Serial.print( "EngLd " ); 
   Serial.print( getobd( 1 ), DEC );
   Serial.print( " %" );
   Serial.println();
'EngLd'

Is a multi-character constant, it is not a string.
For that, you need double quotes.

"EngLd"

But, you can't assign a string to a char.

http://www.cplusplus.com/reference/clibrary/cstring/strcpy/

If you were going to follow the method the original post, it'd have to be

obdesc [0] = 'E'; obdesc [1] = 'n'; obdesc [2] = 'g'; obdesc [3] = 'l'; obdesc [4] = '\0';

But far easier & clearer to do as the previous poster suggested, and just print the literal string with one call.

...is a string constant. It defines a string of characters. (which is what you want) The difference is the quote: single quotes for character constants; double quotes for string constants.

mmmh, "constant" sounds like I cannot change it later - but I need to define it, and then change it several times. Isn't there a way to define a variable char (array)?

It isn't necessary to build the string to print. You can call print and println for each piece...

   Serial.print( "EngLd " );
   Serial.print( getobd( 1 ), DEC );
   Serial.print( " %" );
   Serial.println();

The serial print is just for debugging, in the end I like to send it to a 2x16 LCD. The Code you wrote is exactly what I wanted to send as an output to my display, but my plan was to just call a subroutine what sends the command to obd, takes the result, and put everyting in global variables (an int or float for the value, stings for description and sign behing). Then in the main loop I just need to call the subroutine, and print the char variables, they will (or better: should) always have the right stuff inside. That's my theory.... Your code will work for sure, but I wanted to make the main loop a bit less hardcoded, to implement a menue later, where you can choose what it has to display :) Is there any way to use variable strings? To change them later with other words? I hope it doesn't sound too confusing - my mother language is german, and the friday night wine doesn't make it easier :D

Thanks, guys!

It's really a matter of semantics. You variable obdesc IS a variable. You can change it's contents at any time.

The data that you were trying to put into the variable, "EngLd" is constant. The string will always be the letters E, n, g, L, and d. You can define other strings such as "FailedToGetData", or "SensorFailed", or "EngineMissing", or "MightyFineWine", or "LousyHangover". Those will be constants, too. Especially the hangover after too much of the previous item.

@PaulS
Your wisdom has no end :smiley:

[edit]OP: You can google string literal that is what you tried to assign, and as PaulS says, it is a constant.[/edit]