ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
int RPTSTR(char []. int n) {
//decrease the integer
//store (pointer to ?) string in an array
//repeat until n hits zero. Maybe use recursion? Dunno.
}
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;}
Originally posted by LostSheepOfThePorn I was thinking something along the lines like
#define hooker(y) y*y
#define hooker(y) ((y)*(y))
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.