macro to repeat a token/string n number of times
Hi,
Does anyone know how write a macro that does something like this in C? RPTSTR(str,3) expands to "str", "str", "str" RPTSTR(str,5) expands to "str", "str", "str", "str", "str" Or at the minimum, RPTEMPTY(3) expands to "", "", "" I want to use that macro to fill up an array of strings like this: char *str[] = { "word1", "word2", RPTSTR("dummy string", 5), "word8" }; or char *str[] = { "word1", "word2", RPTEMPTY(5), "word8" }; Thanks in advance for any help. -Sarav |
I don't think (correct me if I'm wrong) that cpp can do that (but I think m4 can).
but you can do: Code:
#define repeat2(s) s, s |
Maybe go something like
int RPTSTR(char []. int n) { //decrease the integer //store (pointer to ?) string in an array //repeat until n hits zero. Maybe use recursion? Dunno. } |
coding it as a function will fail the use case given as an example (try it).
|
Quote:
Thanks, Sarav |
I'm calling off sides on this one. The op wanted to use RPTSTR as
char *str[] = { "word1", "word2", RPTSTR("dummy string", 5), "word8" } (Note in this context, we are using a function) NOT char *str[] = { "word1", "word2", RPTSTR, "word8" } (In this context we are using a Macro) The later would require using #define with RPTSTR(), which is not valid c. |
saravkrish: m4 is a generic macro-processor; it's a lot more expressive that CPP.
see http://www.gnu.org/software/m4/ also useful: #define REPEAT(str, n) RPT ## n (str) // REPEAT("foo", 3) -> RPT3("foo") -> "foo", RPT2("foo") -> "foo", "foo", "foo"; LostSheepOfThePorn: I tried using the identity function for const char* as RPTSTR; gcc -ansi -pedantic -Wall -W -O3 complained: `initializer element is not constant'. if you have a .c-file where RPTSTR is implemented as a function in a way so that you can use it in initializers with a c89-compliant compiler, show us. also, if you have an ansi-compliant compiler that rejects this program, let me know: [code] #define RPT2(str) str, str #define RPT3(str) str, RPT2(str) char* strings[] = {"foo", RPT3("bar"), "baz"}; int main(){return 0;} |
Quote:
|
I was thinking something along the lines like
#define hooker(y) y*y |
Quote:
is safer. I obviously misunderstood your comment that doing this isn't valid C. Why do you assert that RPTSTR("dummy string", 5) has to be a function? What am I missing? |
I don't think you are missing anything. I was probably suffering from terminal confusion at the time. Correct me if I'm wrong. You could write RPTSTR() using #define had it not been for the fact that you use pointers. This might be a crude hack, but you could maybe do something like this.
//Have RPTSTR() return an array of strings //Have array of strings return by RPTSTR() stored in a buffer Now everytime RPTSTR() has a different value, the value in the buffer would change. If buffer was in a list, it would produce the corresponding expansion. Just an idea. Maybe I didn't fully understand the original question. |
I think the idea is to have a compile-time constant; OP could obviously create an empty array of strings on the stack then fill it in at run-time, but if anything else in the code requires a compile-time constant value for the repeated data then having a buffer would not work. I really don't think this is possible with the C preprocessor.
ta0kira |
All times are GMT -5. The time now is 04:08 AM. |