However when I use it with a struct and a Queue (using this Queue library), it fails. I'm not entirely sure where, so posting the whole stripped down code segment:
#include <cppQueue.h>
typedef struct strRec {
//char url[300];
const char* url;
const char* misc1;
} Rec;
cppQueue queue1(sizeof(Rec), 10, LIFO, true);
void setup() {
Serial.begin(115200);
// place a test item in the queue
String url = "http://google.com";
Rec test = {url.c_str(), "some details"};
queue1.push(&test);
}
void loop() {
while (!queue1.isEmpty())
{
// pull that item from the queue
Rec rec;
queue1.pop(&rec);
Serial.println("There's something in the queue!");
Serial.println();
Serial.print(rec.url);
Serial.print(" ");
Serial.print(rec.misc1);
Serial.println();
// None of this works, it's all blank
String url = String(rec.url);
Serial.println("URL as String = ");
Serial.print(url);
Serial.println("URL as c_string = ");
Serial.print( url.c_str() );
Serial.println();
}
delay(1000);
}
The conversions in section below the comment "None of this works" all fail, they all print blank to the console.
I don't imagine anyone can see what I'm doing wrong?
The flow I need to reproduce:
place a String into the Queue (for admittedly bad reasons, it must be a String)
When you don't want others (or yourself) to override existing variable values, use the const keyword (this will declare the variable as "constant", which means unchangeable and read-only):
The Op understands that this constant
was made with 'nothing' in it?
If the thingy was not a const then putting data into the structure would be ```Rec.misc1="some goop.";`'''?
Sorry I should have mentioned, I added the "const" right before posting. Which ever so slightly improved the problem, ha. But it still fails when converting to c_str().
This fails to compile with the dreaded (to me) error "invalid conversion from 'const char*' to 'char*' [-fpermissive]" on the line " Rec test = {url.c_str(), "some stuff"};"
url is a class instance that gets destroyed when it goes out of scope. The function c_str() returns a pointer to a C array inside of this class instance. It does not make a copy, which may or may not be what you want.
Anyway, I don't know what you are trying to do, but all of this is not considered to be best practice. You probably can restructure your program in such a way that these things can be avoided.