I'm writing a library where I allow users to pass different variables, String included.
Since the String library is Arduino specific and not a c++ data type, I'm having a hard time adjusting my code to it.
What I'd like to do is to store a void pointer that can accept a String and print it afterwards (to simplify things).
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
Thanks for your reply.
I am aware of the limitations of the String library and its effects. I'd still want to offer the possibility (to the users) of using it with the library that I'm writing.
etairl:
Since the String library is Arduino specific and not a c++ data type
It has nothing to do with the String class being "Arduino specific". It's just that what you tried to do makes no sense. You generally can't cast a pointer to a type into an object of that type. Also, the Stream class (from which Serial inherits) has no method that accepts a String *. You could do this:
Agreed, OP’s method doesn’t make much sense. How will the function know the type of pointer being passed and how to handle it given it comes through as a void *. That pointer type has valid uses, but this isn’t one of them. Would be better off providing overloaded functions for each type of parameter that can be accepted.
That being said, if I had to use a String, I’d pass a pointer or a reference to the String. Passing the String by value is inefficient if a copy of it has to be made on the stack just to pass it to the function.