In an application
#define IDENTIFIER 'A'
is used. However, the same A needs to be converted to a string.
So instead of having to write
char a[] = "A";
I would like to use the variable in IDENTIFIER to be read into the string array.
How best to write this?
#define IDENTIFIER 'A'
char a[] = IDENTIFIER ;
char a[] = {IDENTIFIER, '\0'} ;
Variables are not defines and defines are not variable.
Declaration of a variable is for informing to the compiler the following information: name of the variable, type of value it holds and the initial value if any it takes. i.e., declaration gives details about the properties of a variable. Whereas, Definition of a variable says where the variable gets stored. i.e., memory for the variable is allocated during the definition of the variable.
I now understand that a define is not a variable.
But I still want to read this IDENTIFIER into an array a[].
I received two answers:
char a[] = IDENTIFIER;
and
char a[] = {IDENTIFIER, '\0'};
What should it now best be?
brice3010:
I received two answers:
char a[] = IDENTIFIER;
and
char a[] = {IDENTIFIER, '\0'};
What should it now best be?
Depends on what you want to do with it.
gfvalvo:
Depends on what you want to do with it.
Send as an array:
LoRa.write((const uint8_t*)&a, sizeof(a));
If you need to treat it as a c-string, then add the null terminator. If not, don't.
gfvalvo:
If you need to treat it as a c-string, then add the null terminator. If not, don't.
Compiling only works if this is used:
char a[] = {IDENTIFIER, '\0'};
If not, an error says that the size of IDENTIFIER could not be determined.
Can't see why. This compiles:
#define IDENTIFIER 'A'
char a[] = {IDENTIFIER};
void setup()
{
Serial.begin(115200);
}
void loop()
{
}
wildbill:
Can't see why. This compiles:
#define IDENTIFIER 'A'
char a[] = {IDENTIFIER};
void setup()
{
Serial.begin(115200);
}
void loop()
{
}
My apologies!! I had forgotten the parenthesis { and }
Doesn't the gcc pre-processor have a "stringify" operator to do exactly what the OP is asking for?
brice3010:
My apologies!! I had forgotten the parenthesis { and }
No need to apologise - that's what was in reply #1 .
RayLivingston:
Doesn't the gcc pre-processor have a "stringify" operator to do exactly what the OP is asking for?
It does, but I'm not certain that's what the OP is asking for.
TheMemberFormerlyKnownAsAWOL:
It does, but I'm not certain that's what the OP is asking for.
If I knew what a gcc-preprocessor or stringify operator areI would answer.
However, using the code above in answer to my question, I do get the correct value at the LoRa receiver.
system
Closed
July 1, 2021, 3:01pm
17
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.