Build a string

Hi all,

I'm gathering data from 6 different sensors. I would like to combine that data into one string that I can send over bluetooth to my mobile phone. The string I want to make will look something like: "123|456|321|25.3|432|e", so I want to separate the values by |-signs. In Processing for example I would do this: String my_string = ""; my_string = 123 + "|"; my_string += 456 + "|"; etc. Or something to that effect. How do you build a string like that with Arduino? Thanks for any help, best, danielle.

You need a char array long enought to hold the longest string you're planning to create.

char myStrint[22]; //can contain 11 chars

Now you need to append to that array, at the indexes you want.

myString[0] = '1';
myString[1] = '2';
myString[2] = '3';
myString[3] = '|';

Or, you could use sprintf maybe itoa.

char myStrint[22]; //can contain 11 chars

sp. "can contain 21 chars"

Thanks, AlphaBeta! I'm sorry but does myString[0] = '1'; count as 1 char? And would '123' count as three? And why can myString[22] only contain 11 chars.

Sorry to sound silly but I'm not very used to working with chars. Thanks for you help, d.

I hadn't read AWOLs' reply. That makes sense, 21. Just wondering about how you count the chars.

I had a typo. It is room for 21 chars, as AWOL indicates. The last index is reserved for the string terminator ('\0', 0, NULL).

myString[0] = '1'; assignes the value of myString[0] to '1' which is 49.

Hi, thanks again. I understand that index of char [0] now holds number 1 but what I'm wondering is: how many room is still left for the other chars? After adding '1' is that 20 or is it 19 as it 49?

Hope you understand what I mean. Because I have to figure out the maximum length of my char.


myString[0] = '1'; assignes the value of myString[0] to '1' which is 49.

I'm confused. It seems to me an array of size[0] could not be valid to assign even a single character string as where would the termination character fit? if size[1], then the '1' would go into array position 0 and the terminating character would go into position 1. Or am I all confused about string arrays in C?

Strings and the C language seems to be not real user friendly, unlike basic language. ;)

I guess a more fundmental question, is a string array of size 0 even valid, or must it have a minimum size of 2 elements?


String handling in 'C' isn't great, but if you think of 'C' as a really high-level assembler for porting an OS (which is what it was designed to be) you won't go far wrong. Was BASIC string handling really that much better? All the MID$ stuff, and not being able to remember whether is was zero or unity origin indexed. {{shudders}}

Was BASIC string handling really that much better?

Well it's been a while, but I just didn't seem to have as much problems using string functions in basic. In C it seems that you have to utilize (and understand) string library functions to get anything useful done. However on the other hand I do seem to recall that most basics restricted the maximum size that a string you be, maybe 256 characters?

Anyway do you have an answers to my prior posted questions about if an array size of 0 is even valid?

Thanks; Lefty

@retrolefty: if you see Reply #1 you find that the myString array has size == 22

On a general note; I think the out-of-the-box string handling is bad. But, as you have total control and a powerful language to your disposal, one could always write a String class/library to suit your exact needs.

I have learned to love it.

[edit]An array of size 0 does not allocate any memory but I think this:

char str[] = "test";
char str2[0];
str2 = str;


Ought to work.

While writing I became insecure but... I think it works.[/edit]

@Lefty: Yes, you can declare a zero-length array:

char array [0];

but you can't initialise it char array [0] = {1};, though you can assign to it, array [0] = 1; I guess because of the array/pointer duality. Funny langauge, innit?

char array [0]; == char *array;

char str[] = "test";
char str2[0];
str2 = str;


That really is hard for me to understand. I tried to compile that and got the following:

error: incompatible types in assignment of 'char [5]' to 'char [0]'


Need some & in there. Got to run, will be back with fix :)

[edit] In MS Visual Studio this is the only compiling solution:

char ar[5] = "test";
char *arr;

arr = (char*)&ar;

It seems to be illegal to declare an array of length 0 with the Visual C++ compiler.[/edit]