String library - is it possible to convert void pointer to String?

Hi,

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).

The error that I come accorss is:

conversion from ‘void*’ to ‘String’ is ambiguous

Here's the code:

String x = "Hello World";

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

void loop() {
 f(&x);
}

void f(void *y) {
 Serial.print(String(y));
}

Now, obviously if we replace the String data type with an integer or a char array, we have on problem since we can cast these data types:

Serial.print((char*)y);

or

Serial.print((int)y);

^ These would work just fine. Is it somehow possible to cast a String data type or in some other way convert a void pointer to a String?

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).

...R

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:

#include <Arduino.h>
void f(void *y);

String x = "Hello World";

void setup() {
 Serial.begin(115200);
 delay(1000);
 f(&x);
}

void loop() {
}

void f(void *y) {
 Serial.print(((String *) y)->c_str());
}

Or, even simpler:

#include <Arduino.h>
void f(void *y);

String x = "Hello World";

void setup() {
 Serial.begin(115200);
 delay(1000);
 f(&x);
}

void loop() {
}

void f(void *y) {
 Serial.print(*((String *)y));
}
void f(void *y) {
 Serial.print(String(y));
}

If you KNOW that the argument to f() CAN be treated as a String object, why not just declare f() as taking a String?

If you plan to have the function guess what type was passed to it, why does it currently assume that conversion to a String is possible?

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.

Since the String library is Arduino specific and not a c++ data type

String is NOT Arduino specific!.

You cannot convert a pointer to anything other than a pointer!

Mark