String concatenation and printing

Hi,
I have 2 strings in a mixed struct.
The strings are defined in the struct as char string[x], and given string values.
To print out, I concatenate several strings into one longer string, and print it out via serial print command.
So far, so good.
Problem is that while it printed correctly in previous versions of my code, it does not print in a new version, with very little change from previous.
Unfortunately, I could not figure out what made the change.

Here is my code snippet:

#include <string.h>
.
.

struct defineConfigs {
.
char configVersion[4];
char versionDate[11];
. (more items in the struct)
.
.
}
configData = {
.
"B60",
"22-05-15",
.(more items)
.
}

The print command in the setup():

Serial.println((String)"Rev " + configData.configVersion + " " + configData.versionDate);

While in the previous version it printed out the strings as they appear, namely "Rev B60 22-05-15"
in the new version it always prints out "Rev 32".

I send the same string to the lcd, and get the same outcome.

There seem to be no relevant difference between the versions, and yet it will always print "Rev 32"
(32 is the ascii code for space(?))

What i the correct way to define and concatenate the strings and make them always print correctly?

Thanks

Serial.println((String)"Rev " + configData.configVersion + " " + configData.versionDate);

Quit being lazy. Use as many Serial.print() statements as needed to output each piece of data. Quit pissing away resources (ab)using the String class that way.

There is also the function sprintf(). You should think about looking into it.

Thanks for all suggestions.
Eventually I found out there was no problem with the printing code, but rather with the data in the structure.
The suggested format using String concatenation is just fine, and has nothing to do with laziness.
It is a useful tool that was added to Arduino, and is the best choice.
(Such tool is available in all higher languages I know)

I use it for the simple reason that I re-use the same text in several locations, thus building a single string and re-using it is the correct approach.

In my code I use it: (and it is just fine)
String message = (String)"Rev " + configData.configVersion + "," + configData.versionDate;
Serial.println(message);
lcd.print(message);

samtal

It is a useful tool that was added to Arduino, and is the best choice.

No it is not. At least not on an Arduino which has very limited memory. C strings (char arrays) are the preferred choice. You might have to write more, but they save you SO much more memory than Strings.

It is a useful tool that was added to Arduino, and is the best choice.

Nonsense.

(Such tool is available in all higher languages I know)

Where such languages are used on hardware with 16 gigs of real memory and an operating system that makes almost unlimited amounts of virtual memory available, such tools are fine. On an Arduino, with no OS, and a very small amount of memory, such tools are for fools.

I don't think that the OP understands the nature of serial data, limited RAM or C++ container classes.

Serial.println((String)"Rev " + configData.configVersion + " " + configData.versionDate);

can be better written as

Serial.print("Rev ");
Serial.print(configData.configVersion);
Serial.print(" ");
Serial.println(configData.versionDate);

to use minimal memory.

Optionally you wrap it in a separate function:

printVersionInfo()
{
  Serial.print("Rev ");
  Serial.print(configData.configVersion);
  Serial.print(" ");
  Serial.println(configData.versionDate);
}

Hi samtal - not sure why such critical-toned responses to your post. It may be good to hear there are better ways for this environment, but your question at least should be considered.

I'm new to Wiring language and confused by these examples offered in Arduino documentation:

Caution: You should be careful about concatenating multiple variable types on the same line, as you may get unexpected results. For example:

int sensorValue = analogRead(A0);
String stringOne = "Sensor value: ";
String stringThree = stringOne + sensorValue;
Serial.println(stringThree);

results in "Sensor Value: 402" or whatever the analogRead() result is, but

int sensorValue = analogRead(A0);
String stringThree = "Sensor value: " + sensorValue;
Serial.println(stringThree);

gives unpredictable results because stringThree never got an initial value before you started concatenating different data types.

https://www.arduino.cc/en/Tutorial/StringAdditionOperator

Both code snippets would work fine in Java so I'm wondering whatthe Wiring compiler does differently, or if the second example is a mistake.

(Point taken that the String class and concatenating strings in this way carries overhead that may be undesirable on limited resource machines).

sb44:
not sure why such critical-toned responses to your post.

We discourage bad practices. It helps to cut down on Arduino beginners developing BAD HABITS that end up with them trying to figure out why their sketch crashed.

String class is okay on my computer so it's okay on Arduino is about puddle deep. That's like saying my house has a bathtub so my bicycle can have one too. Even empty it's a kludge. Let's not teach beginners to put bathroom fixtures on bicycles even if we can.

Sure, I know I can use all the guidance I can get. I just don't see what mean-spirited tone accomplishes.

You see how you interpret.

BTW, this is a tech forum not a modern university "safe space" with cuddle toys and blankets.

Oh, wow, yeah mean spirited is the way I read it too.
You don't know these guys background so why be so heavy handed.
I don't think you helped much.
The guy might never want to post again.
If you want to play with the big boys go over to the stm32 forum and hang out there.

sb44:
Sure, I know I can use all the guidance I can get. I just don't see what mean-spirited tone accomplishes.

I don't see what "I'm sticking to this way even if you all tell me it's a mistake" gets you.

Members took time to tell you better and you blew them off then play victim when they try to wise you up.

But hey, you waste all of your own time you want, I bet you don't get upset over that.

samtal:
Thanks for all suggestions.
Eventually I found out there was no problem with the printing code, but rather with the data in the structure.
The suggested format using String concatenation is just fine, and has nothing to do with laziness.
It is a useful tool that was added to Arduino, and is the best choice.
(Such tool is available in all higher languages I know)

I use it for the simple reason that I re-use the same text in several locations, thus building a single string and re-using it is the correct approach.

In my code I use it: (and it is just fine)
String message = (String)"Rev " + configData.configVersion + "," + configData.versionDate;
Serial.println(message);
lcd.print(message);

samtal

The reason you were steered away from using String is because the ARV chips have very little RAM and the way that String works is a very bad fit that causes lots of problems that the ones of us that steer you away from the practice have helped many others fix their code.

Perhaps you did not know but that does not help YOU one bit with your code. Your ego, maybe, but that just holds YOU back.

"The suggested format using String concatenation is just fine, and has nothing to do with laziness.
It is a useful tool that was added to Arduino, and is the best choice.
(Such tool is available in all higher languages I know)"

Funny how you dismissed the reasons you were given to not use String but instead go straight to "laziness". The one thing you can object to is the thing you base your choice on.

I've typed out full explanations for why not to use String many times but I do that for people who ask in words that say they are ready to learn something. Of course it helps after they've painted themselves in a corner and are ready to learn.

A little bit of "know" lets you compare how to code modern PC's to AVR microcontrollers because languages.

I've been helping members here for over 6 years. When I tell you something is going lead you to trouble, that's because it will.