As my team is still using ROBOTC for now, I have been trying to experiment around with making some of our code easier to use, specifically by putting the instructions for our Pre-Autonomous LCD code in an array. In short, I found that
(char *)"this is a string"
will produce a compiler error, whereas
char *variable = "this is a string;
does not. Type punning (in a weird way) however, works Examples below:
// Doesn't work, "**Error**:Invalid cast type from 'string' to 'long'"
char *foo = (char *)"bar";
// Works... oddly
char *foo = "bar";
// Also works, I guess because the 'string' type is really a 20-byte integer of sorts. However, it is unsightly and confusing
char *foo = (char *)&"bar";
// Arrays of strings don't work though, "**Error**:Illegal assign of char string to a non-pointer value"
char *strings] = {
"foo",
"bar",
}
// One hack for arrays and structs of strings
// Compiler complains about embedded '='s
char *rstocs;
char *strings] = {
rstocs = "foo",
rstocs = "bar",
}
// Another hack for arrays and structs of strings
// Compiler complains of possible illegal use of '&' operand
char *strings] = {
(char *)&"foo",
(char *)&"bar",
}
// Best solution right now is probably with a macro
#define cstr(S) ((char *)&(S))
char *strings] = {
cstr("foo"),
cstr("bar"),
}
Either way, no programmer should have to dig for hacks like these. Please fix it.