easy way to convert a string to a character array

is there an easy way to convert a string into a character array sort of like...

String S1 = "hello";

char array[] = S1:

or do I have to use a for or while loop.

There's this:

But really, it's better to just not use String at all.

is there an easy way to convert a string into a character array sort of like...

A string IS a char array, so no conversion is needed.

A String, on the other hand, has no place on an Arduino, so there is no need to convert something that shouldn't exist.

I do not understand why you would say that a string has no place on an Arduino. There are a bunch of string functions that are useful when dealing with parsing keyboard inputs. For example adding two strings is a lot easier than adding two arrays. It seems to me that the string functions eliminate a lot of code to write if only using arrays.

I have a function that claimed to accept both a string or array as a data input. Unfortunately, the function worked with an array and some but not all strings. I have not dug into the function to see where the problem is. I just converted the string to an array and continued.

barryjo:
I do not understand why you would say that a string has no place on an Arduino.

there is too much available information for a redux on this thread, but suffice it to say that using String class can become disruptive to the heap if you are doing a lot of String manipulation (i.e. concatenation).

If you want to learn more, here is a good description of why it is a problem...

I get it. I read the article you posted. What you are saying, I think, is to not use the string class but rather just use the C string functions instead.

I will do so. Thanks

For example adding two strings is a lot easier than adding two arrays.

Nonsense.

  String wasteOfMemory = "What a waste";
  String justPissItAway = "There's always more";

  String bunchOfCrap = wasteOfMemory + justPissItAway;
  char some[10] = "ABCD";
  char *rest = "1234";

  strcat(some, rest);

Explain to me how the first block of code is easier than the second.

Now, if your statement was "I know how to concatenate Strings but I don't know how to concatenate arrays", I'd have a different answer.

That would be "Learn!".

Well, PaulS, I used to be a C programmer and knew all of the C string functions. It seems that now, the modern direction is to go to object orientated programming using classes. In your example above, the new way is to use the classes, the old way is to do "old"C. Granted, the old way might be more efficient but is harder to understand. Assuming that my computer has lots of memory, as most of them do these days, not Arduino of course, and assuming that speed is not an issue, then I would think using the class method would be better and easier to understand.

While knowing how to use pointers in C is an efficient method, classes are more understandable. I wonder, does Java use pointers or classes?

Assuming that my computer has lots of memory, as most of them do these days, not Arduino of course, and assuming that speed is not an issue, then I would think using the class method would be better and easier to understand.

It's that "not Arduino of course" part that distinguishes whether or not to use Strings. If you have gobs of memory, then feel free to use them. If not, as the Arduino doesn't, then don't.

ALL of the String functions rely on string functions to actually work, so why not just learn to use the string functions?

Years ago, maybe 15, before C++ (OOP). the string functions were all that was available and I used them. Now with the OOP languages like Java and Python I tend to want to use classes wherever I can so my knowledge of classes will transfer to other OOP languages. Continuing to use the C functions keeps me tied to C.

Hey, are you an expert on interfacing a USB joystick to the Arduino? I need help here.
Yes, I have acquired the USB library and have successfully connected a keyboard but not my joystick.

You have a bad habit of referring to String as string. I realize it's confusing that the two are so similar but it's very important to understand the difference and be clear in your writing.

I have a lot of bad habits unfortunately. I thought String is a class where you can instantiate any identifier as a String, i.e. String mystring. then you can use the class procedures to deal with mystring.

I realize that the class String is not the same as what is called a string in C where a string is an array on characters terminated by a 0.

If I was clear on all of these issues I would not be here in this forum.

Years ago, maybe 15, before C++ (OOP)

Try a bigger number.

I learned as a procedural programmer starting 54 years ago. It took me a long time to change to OOP and I don't know for sure that I am there as yet.

barryjo:
I learned as a procedural programmer starting 54 years ago. It took me a long time to change to OOP and I don't know for sure that I am there as yet.

You are not, if you insist that every bit of code MUST be written using objects.

OK, I will assume that using strings for Arduino is not good because of excessive memory usage.

Does this mean that using strings is bad for other computers? Can I use strings with a Raspberry Pi, or a Mac or a PC with no issue? Is the problem a result of the string library of the Arduino being poorly written and that string libraries for other products might be better?

Just wondering.

barryjo:
Does this mean that using strings is bad for other computers? Can I use strings with a Raspberry Pi, or a Mac or a PC with no issue? Is the problem a result of the string library of the Arduino being poorly written and that string libraries for other products might be better?

Why then would such a class exist at all?

For computers with 1) a large memory pool and 2) proper garbage collection things like String classes and Vectors are not a problem. All types of dynamic allocation are done every day on computers.

You can use strings on Arduino, Pi, Mac, PC no problem. They definitely don't cause excessive memory use on Arduino. Obviously you don't want to use unnecessarily long strings if you can avoid it and use the F() macro to put string literals in flash when possible. It would be practically impossible to do most anything without strings.

An earlier post referenced an article explaining why not to use strings in the Arduino because of memory issues. So, does the arduino use a different C++ compiler and li9braries than would be used on a PC for example? When the Arduino compiles, I see the "gcc" compiler shown often. Isn't this the same compiler that Linux uses? What happens to the compiled code as I move a program from the Arduino to a PC?

I am just trying to understand.

barryjo:
An earlier post referenced an article explaining why not to use strings in the Arduino because of memory issues.

I'm going to need to see a reference on that. I can't imagine someone every saying not to use strings because of memory issues. Of course you do need to be careful to always add the terminator and not write past the end of an array but that doesn't mean don't use them.