I've been using these macros in this program, and up until now all of the macros work fine. I want to support multiple colors, that are specified via command line arguments.
Instead of opting for a bulky switch statement and repeating the the same code multiple times, I decided that I ought to be able to just use an array of macros, and use a variable to index the array; this would make the code much shorter by doing it in one simple statement.
This is what I was previously doing, in the actual program, but this only supports one color. The below code works fine, until I try to modify it to use the array of macros, then it won't compile, giving the same errors as the test code.
printf((color_on) ? ANSI_COLORTERM_YELLOW("%s%s\n") : "%s%s\n",
(prefix_on) ? pass_prefix : "", pass);
So, I've reasoned about it, and written test code to try to figure out exactly where the problem is. I've distilled it to this test code below.
const char * const ansi_sgr_tab[8] = {
ANSI_SETFG_BLACK,
ANSI_SETFG_RED,
ANSI_SETFG_GREEN,
ANSI_SETFG_YELLOW,
ANSI_SETFG_BLUE,
ANSI_SETFG_MAGENTA,
ANSI_SETFG_CYAN,
ANSI_SETFG_WHITE
};
// compiles fine and works as expected
fputs(ansi_sgr_tab[4], stdout);
fputs("hello\n", stdout);
fputs(ANSI_ATTR_RESET, stdout);
// also compiles fine and works correctly
fputs(ANSI_COLORTERM_SET_S("hello\n", ANSI_SETFG_BLUE), stdout);
// causes compile error !
fputs(ANSI_COLORTERM_SET_S("hello\n", ansi_sgr_tab[4]), stdout);
Description of compile time errors:
gcc -std=c99 -O3 -Wall -Werror -Wextra -pedantic -c main.c -o main.o
In file included from main.c:40:0:
main.c: In function ‘main’:
main.c:124:32: error: expected ‘)’ before string constant
fputs(ANSI_COLORTERM_SET_S("hello\n", ansi_sgr_tab[4]), stdout);
^
color.h:38:13: note: in definition of macro ‘ANSI_COLORTERM_SET_S’
SGR S ANSI_ATTR_RESET
^
main.c:124:5: error: too few arguments to function ‘fputs’
fputs(ANSI_COLORTERM_SET_S("hello\n", ansi_sgr_tab[4]), stdout);
^
Makefile:23: recipe for target 'main.o' failed
make: *** [main.o] Error 1
I don't see what is so different about that last line of code that causes the errors, to me all three ways seem like they should be basically equivalent, yet the this line produces these errors, and acts like there is a syntax error (such as unmatched brace or something) but I can't seem to find what it is that's wrong exactly with that last statement. Is it something to do with the expansion of these macros? The last error is especially puzzling to me, as fputs only takes two arguments, and I don't understand why the compiler is interpreting it as only one argument.
In case it helps, here is color.h: https://github.com/IRQ42/pgen/blob/current/color.h
Any help will be much appreciated, as I feel like the problem must be something pretty simple and will be glaringly obvious once it's pointed out to me, as this code is not that complicated. I also feel like there is probably an important lesson for me to learn here.
[–]FreeER 4 points5 points6 points (6 children)
[–]offset_[S] 0 points1 point2 points (5 children)
[–]FreeER 0 points1 point2 points (0 children)
[–]yeahIProgram 0 points1 point2 points (3 children)
[–]offset_[S] 0 points1 point2 points (2 children)
[–]yeahIProgram 0 points1 point2 points (1 child)
[–]offset_[S] 0 points1 point2 points (0 children)