I might be off-base here but why not "define" it as a const char, rather than a macro? RP On Mon, 30 Sep 2019 at 10:46, toft wrote: > You are confusing types, which is super easy to do as macros don't have > a type that the compiler can check... > > #define STRING_PREAMBLE "@" > > means that whereever STRING_PREAMBLE is found in the code it's replaced > by "@" which is esentially a pointer to a character > > the array concatenation is done during compile time and only works with > literals (which the macro expands to) > > this line > if (payload[0] =3D=3D STRING_PREAMBLE) > > is comparing payload[0] (the contents of a pointer to the first element > of an array) with a pointer to the first element of a different array. > The compiler probably promotes the character to an int and compares the > integer to the pointer. > > What you probably need is... > if (payload[0] =3D=3D STRING_PREAMBLE[0]) > > > > > On Mon, 2019-09-30 at 02:37 +0800, Justin Richards wrote: > > Isaac, > > > > I did indeed miss that in your previous post. > > > > Thank you for your input and testing.efforts. > > > > > From your findings I think I can safely dismiss this as a non > > > viable and > > suspect it will only further obfuscate the code. > > > > Cheers Justin . > > > > > > > > On Mon, Sep 30, 2019 at 1:56 AM Isaac M. Bavaresco < > > isaacbavaresco@gmail.com> > > wrote: > > > > > I think you missed that in my first post: > > > > > > #define STRING_PREAMBLE @ > > > #define STRINGIFY_(c) #c > > > #define STRINGIFY(c) STRINGIFY_(c) > > > > > > Then: > > > > > > char *pidupdate=3DSTRINGIFY(STRING_PREAMBLE) ID "," posVal; > > > > > > You can stringify using the preprocessor # operator, buried under > > > two > > > levels of macro expansion. > > > > > > I did some tests and unfortunately it seems that it is not possible > > > to > > > convert a macro contents into a single-quote quoted character. > > > > > > Cheers, > > > Isaac > > > > > > > > > Em dom, 29 de set de 2019 14:23, Justin Richards < > > > justin.richards@gmail.com> > > > escreveu: > > > > > > > I am not really sure how to STRINGFY. The gcc help file sect 3.4 > > > > Stringification gives examples using double quotes. > > > > > > > > I tried defining without quotes which works ok when later > > > > wrapping with > > > > double quotes > > > > > > > > but this character comparison fails > > > > > > > > #define STRING_PREAMBLE @ > > > > if (payload[0] =3D=3D 'STRING_PREAMBLE' ) > > > > > > > > I think it is comparing payload[0] with the first char after the > > > > single > > > > quote i.e 'S' > > > > > > > > and the compiler does not like double quotes as below > > > > > > > > if (payload[0] =3D=3D "STRING_PREAMBLE" ) > > > > > > > > nor does it like this > > > > > > > > if (payload[0] =3D=3D STRING_PREAMBLE) > > > > > > > > > > > > On Sun, Sep 29, 2019 at 10:38 PM Isaac M. Bavaresco < > > > > isaacbavaresco@gmail.com> wrote: > > > > > > > > > You cannot define STRING_PREAMBLE's at sign with double quotes. > > > > > > > > > > Define it without quotes and STRINGIFY it to use in the string > > > > > concatenation, it will work. > > > > > > > > > > Then in the 'case', use: > > > > > > > > > > case 'STRING_PREAMBLE': > > > > > > > > > > It should work. > > > > > > > > > > Cheers, > > > > > Isaac > > > > > > > > > > > > > > > Em dom, 29 de set de 2019 11:28, Justin Richards < > > > > > justin.richards@gmail.com> > > > > > escreveu: > > > > > > > > > > > That line is actually part of a string that contains > > > > > > javascript that > > > is > > > > > > embedded in html that is sent to the browser client when > > > > > > requested. > > > > > > > > > > > > so the plus signs are actually in the javascript code. > > > > > > > > > > > > server.sendContent(F("var arrpv=3D[];" > > > > > > ... > > > > > > "var pidupdate=3D' " STRING_PREAMBLE " ' +ID+ ' , ' > > > > > > +posVal; > > > > > > console.log('pidupdate:'+pidupdate); " > > > > > > ... > > > > > > ")); > > > > > > > > > > > > I guess it would have been clearer if I said I want to do > > > > > > something > > > > like > > > > > > this > > > > > > > > > > > > #define STRING_PREAMBLE "@" > > > > > > > > > > > > strHTML[] =3D "sometext " STRING_PREAMBLE "someothertext" ; > > > > > > > > > > > > As best as I can tell the above expands to > > > > > > > > > > > > strHTML[] =3D "sometext " "@" "someothertext" > > > > > > > > > > > > and effectively produces > > > > > > > > > > > > strHTML[] =3D "sometext @someothertext" > > > > > > > > > > > > and works ok until I try > > > > > > > > > > > > if (payload[0] =3D=3D STRING_PREAMBLE) > > > > > > > > > > > > which expand to > > > > > > > > > > > > if (payload[0] =3D=3D "@") > > > > > > > > > > > > which the compiler does not like. > > > > > > > > > > > > I am using the Arduino IDE to compile code for the > > > > > > ESP8266. As best > > > > as i > > > > > > can tell the IDE/compiler accepts both C and C++ > > > > > > > > > > > > justin > > > > > > > > > > > > On Sun, Sep 29, 2019 at 8:35 PM Isaac M. Bavaresco < > > > > > > isaacbavaresco@gmail.com> > > > > > > wrote: > > > > > > > > > > > > > Are you using C++? > > > > > > > > > > > > > > Because in > > > > > > > > > > > > > > pidupdate=3D'"STRING_PREAMBLE"'+ID+','+posVal;console.log('pi > > > > > > > d > > > > > > > update:'+pidupdate) > > > > > > > > > > > > > > The plus signs are not valid in C to concatenate strings, > > > > > > > also, if > > > > > posVal > > > > > > > is a variable then even without the plus signs it would not > > > > > > > work. > > > > > > > > > > > > > > In C you could write: > > > > > > > > > > > > > > #define STRING_PREAMBLE @ > > > > > > > #define STRINGIFY_(c) #c > > > > > > > #define STRINGIFY(c) STRINGIFY_(c) > > > > > > > > > > > > > > Then: > > > > > > > > > > > > > > char *pidupdate=3DSTRINGIFY(STRING_PREAMBLE) ID "," posVal; > > > > > > > > > > > > > > I just noticed you are using single quotes for some > > > > > > > elements of the > > > > > > > expression. I'm pretty sure you must be using C++. > > > > > > > > > > > > > > C++ is not C. They have important differences. > > > > > > > > > > > > > > Cheers, > > > > > > > Isaac > > > > > > > > > > > > > > > > > > > > > > > > > > > > Em dom, 29 de set de 2019 05:03, Justin Richards < > > > > > > > justin.richards@gmail.com> > > > > > > > escreveu: > > > > > > > > > > > > > > > Using macros to make some code more readable and > > > > > > > > maintainable I > > > > > always > > > > > > > seem > > > > > > > > to hit snags. > > > > > > > > > > > > > > > > Struggling to define the question, I have used examples > > > > > > > > below to > > > > try > > > > > > and > > > > > > > > illustrate my query. > > > > > > > > > > > > > > > > I wanted to convert this > > > > > > > > > > > > > > > > if (payload[0] =3D=3D '@') > > > > > > > > { > > > > > > > > ... > > > > > > > > } > > > > > > > > > > > > > > > > to > > > > > > > > #define STRING_PREAMBLE '@' > > > > > > > > > > > > > > > > if (payload[0] =3D=3D STRING_PREAMBLE) > > > > > > > > { > > > > > > > > ... > > > > > > > > } > > > > > > > > > > > > > > > > The compiler complains with "expected ')' before '@' > > > > > > > > > > > > > > > > So i tried variations on the theme > > > > > > > > #define STRING_PREAMBLE @ > > > > > > > > #define STRING_PREAMBLE "'@'" > > > > > > > > #define STRING_PREAMBLE "@" > > > > > > > > > > > > > > > > The later two variations compiles but the following > > > > > > > > comparison > > > > fails > > > > > > > > if (payload[0] =3D=3D 'STRING_PREAMBLE') > > > > > > > > > > > > > > > > The macro is used elsewhere in the code as part of a > > > > > > > > string of > > > > > embedded > > > > > > > > HTML that embeds javascript and behaves as required > > > > > > > > "var > > > > > > > > > > > > > > > > > > > pidupdate=3D'"STRING_PREAMBLE"'+ID+','+posVal;console.log('pidupdate: > > > '+pidupdate);" > > > > > > > > this was previously coded as > > > > > > > > > > > > > > > > "var > > > > > pidupdate=3D'@'+ID+','+posVal;console.log('pidupdate:'+pidupdate) > > > > > ;" > > > > > > > > Any hints or suggestions on how i should phrase my > > > > > > > > search. > > > > > > > > > > > > > > > > Justin > > > > > > > > -- > > > > > > > > http://www.piclist.com/techref/piclist PIC/SX FAQ & list > > > > > > > > archive > > > > > > > > View/change your membership options at > > > > > > > > http://mailman.mit.edu/mailman/listinfo/piclist > > > > > > > > > > > > > > > -- > > > > > > > http://www.piclist.com/techref/piclist PIC/SX FAQ & list > > > > > > > archive > > > > > > > View/change your membership options at > > > > > > > http://mailman.mit.edu/mailman/listinfo/piclist > > > > > > > > > > > > > -- > > > > > > http://www.piclist.com/techref/piclist PIC/SX FAQ & list > > > > > > archive > > > > > > View/change your membership options at > > > > > > http://mailman.mit.edu/mailman/listinfo/piclist > > > > > > > > > > > -- > > > > > http://www.piclist.com/techref/piclist PIC/SX FAQ & list > > > > > archive > > > > > View/change your membership options at > > > > > http://mailman.mit.edu/mailman/listinfo/piclist > > > > > > > > > -- > > > > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive > > > > View/change your membership options at > > > > http://mailman.mit.edu/mailman/listinfo/piclist > > > > > > > -- > > > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive > > > View/change your membership options at > > > http://mailman.mit.edu/mailman/listinfo/piclist > > > > > -- > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .