Even char / string problem

Hi,

I received String (A), the lengh is not even the same.

I need to add some char into this string. We can to that easely with string but how to do taht with char?

Thanks

char SUP[2] = "km";

void modif(char* A)
{
  int len = strlen(A);
  char txt[len+2];
  strncpy(txt, SUP, 2);
  strncat(txt, A, len);
}

www.xyproblem.info

"...not even the same" The same as what?

Usually the method would be to declare the string to be big enough to hold the largest item expected. The items don't all have to fill the string entirely every time.

Before putting anything into a fixed-length string, make sure that it isn't longer than the string.

char SUP[2] = "km";
Is not a char. It is a char array containing 2 bytes of space. One containing the k, the other containing the m.
Becouse there are only two bytes of space declaired you can not add anything to it. If you want to add to it you should make something like this:
Char SUP[16] = "km";
This places the k in the first byte, the m in the second byte and leaves 14 bytes for you to add data to.
I hope this helps,
Elvenstof

char SUP[2] = "km";How many bytes does it take to save the string ? Is it 2 or 3 ?

Details about cstrings and the relevant functions can be found here.

...R

Thanks to all for your inputs!

@elvenstof It's a char array! A could be until 64 characters. Also Char SUP[66] = "km";

But the result wanted is into the char txt, am I right ?

My problem is how to pass A because I received A as a string and the modif use char? (Convert string into char array)

Thanks

Time for all the code or at least a compilable example :wink:

You are right septiilion!

There is:

char SUP[3] = "km";
//char TXT1[]="123456";  // OK 
String TXT1 = "123456";  //NOT OK; length could be up to 64 

void setup() 
{
  Serial.begin(9600);
  delay(500);
}

void loop() 
{
  modif(TXT1);
  while (1)
  { 
  }
}


void modif(char* A)
{
  int len = strlen(A);
  char txt[len+2];
  strncpy(txt, SUP, 2);
  strncat(txt, A, len);
  Serial.println(txt);
}

This might help:

If you have a string, for example:

String SUP[3] = "km";

You can use setCharAt(); to add to the String.
And you can use charAt(); to read the characters in the String.

Example:

String SUP[3] = "km";
SUP.charAt(0);   //this will return 'k'
SUP.setCharAt(1, 'b');   //this will replace 'm' with 'b' resulting in: SUP = "kb"
SUP.setCharAt(2, 'c');   //this will add 'c' to the String resulting in: SUP = "kbc"

I hope this helps.
-elvenstof

And now the million dollar question, why do you want to? On a micro you rarely need to do that? Because most of the times we just want to output that string somehow. Then simply output the two part right after each other :wink:

This thread seems to be full of the common confusion of Strings (objects created using the String library) and C style strings (zero terminated arrays of chars)

Both methods have functions to manipulate the variables but the functions are not generally interchangeable.

The first decision is whether to use Strings or strings. The latter are generally preferred when using an Arduino because they do not lead to memory fragmentation. Having made the decision then suitable variables of the correct type need to be declared. In the case of strings allowance must be made for the terminating zero which takes up one extra byte in the char array over and above the characters, hence my question in reply #3

Once the data is in the variables then the appropriate functions can be used to find the length of a string (or String), the position of characters within it and to concatenate or split them as required. Robin's post #4 has a link to the functions available to manipulate strings.

Some of the code posted in this thread does not compile, even the humble

char SUP[2] = "km";

which I believe exposes some of the confusion.

So, which is it to be Strings (possibly inefficient) or strings (often seen as complicated but in reality not so)

char SUP[2] = "km"; will compile fine. The trailing nul will be "squeezed out" and not included. You can't use it in any functions that count on a trailing '\0', but it might be fine for other purposes.

KeithRB:
char SUP[2] = "km"; will compile fine.

That's valid in C, but not in C++.

error: initializer-string for array of chars is too long [-fpermissive]
 char sup[2] = "km";
               ^

strncpy(txt, SUP, 2); will not copy the terminating nul character of SUP (assuming SUP is now three characters) and hence you can not use strncat(txt, A, len);

The size of your (lower case) txt variable is also one to small. Count the characters 6 + 2 equals 8 and hence no space for the nul terminator.

Oops, you are right, you have to use the much more explicit:
char SUP[2] = {'k', 'm'};