Hello,

On Sat, 30 May 2020, Patrick Hammer wrote:

#include <stdio.h>
#define abc "abc"

int main(void)
{
    char a[2] = { abc[0], 0 };
    puts(a);
    return 0;
}

Output:
arraystring.c:6: warning: initializer-string for array is too long
arraystring.c:6: error: '}' expected (got "[")

Interestingly this program works when a parenthesis is put around abc[0]:
char a[2] = { (abc[0]), 0 };
Alternatively, it also works if the parenthesis is around the defined string
instead:
#define abc ("abc")
which can also be done at usage level:
char a[2] = { (abc)[0], 0 };

so maybe it's an issue with the parser?

Yes it is, specifically the parser for initializers. It tries to special-case string initialization to not use the general expression parser, and that bites us here. (The reason for that is to not commit the string to memory too early when it's used to initialize some static data) We really need to go through the normal expression parser for this case (which is why it works when adding parentheses around the string token), but that needs some work to not break other cases.


Ciao,
Michael.
_______________________________________________
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to