Skip to main content
gamedev.net
🔒 Locked

Even more #define macros

Started by Rob Loach Mar 17, 2005 at 8:10 PM 2 replies 500+ views
Original Post
Rob Loach
Rob Loach
Is it possible to get const char arrays through a macro? Something like this:
#define CrazyFunction(x) void  ## x ## (int b)

// Then for a function definition you'd go:
CrazyFunction("Hello"){
    // Function code
}
Or is it only possible with numbers?
Rob Loach [Website] [Projects] [
Evil Steve
Evil Steve
Nope, you can't do that. However, you can pass Hello to the function, and use #x if you need to convert to a const char*

EDIT: Some code:
#define Func(x) printf("%s",#x);Func(Hello)

Ok, that's a really bad example :P
Sneftel
Sneftel
I'm confused.... are you trying to de-stringize a string-constant with the preprocessor? If so, why the heck do you want to do this? The stringizing operator may be sufficient for you.
Drew_Benton
Drew_Benton
[lol] Rob, here's a start for you:

#include <stdio.h>#include <stdlib.h>#define CF1(x) void _ ## x ## (int b)#define CF2(x,y) CF1(x) { printf("%i",y); abort(); }CF2(HI,2)int main( int argc, char** argv ){	_HI(0); // 0 does not work b/c of the 2 above	return 0;}


You were close to being with, but see what I did to get around the error you had, with the "_" and space?[wink]. I think I know what you are trying to do, and I think I had tried it before and could not get it to work, but then again, I could not of had made this code work either. Something to play with. I will work with this though, I think you may be able to do some intresting stuff with this.

- Drew

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.