Welcome! Log In Create A New Profile

Advanced

preproccessor question

Posted by g_man 
preproccessor question
November 04, 2010 05:52AM
I'm new to using the preproccessor, and I have a problem. I am trying to take an argument and convert it to a string and get it's value, but it doesn't work. That was probably confusing so here is my code:
#define SENDVARIABLES(x,arg1,arg2,arg3,arg4,arg5,arg6)	\
do{	\
if(arg1!=NOTAVAR)	\
	addVariableDouble(x*6,arg1,(char*)"Player 2 arg1 x");\
if(arg2!=NOTAVAR)		\
	addVariableDouble(x*6+1,arg2,(char*)"Player 2 arg2 x");	\
if(arg3!=NOTAVAR)		\
	addVariableDouble(x*6+2,arg3,(char*)"Player 2 arg3 x");	\
if(arg4!=NOTAVAR)		\
	addVariableDouble(x*6+3,arg4,(char*)"Player 2 arg4 x");	\
if(arg5!=NOTAVAR)		\
	addVariableDouble(x*6+4,arg5,(char*)"Player 2 arg5 x");		\
if(arg6!=NOTAVAR)		\
	addVariableDouble(x*6+5,arg6,(char*)"Player 2 arg6 x");\
} while(0)
I am trying to get the value of x , and arg1-6 in the first two arguments of addVariableDouble, and in the last one I want the to be strings. When I run it now the first two arguments are fine, but the string turns out as "Player 2 arg1 x"
Thanks
Re: preproccessor question
November 04, 2010 10:01AM
You are looking for Stringification: [gcc.gnu.org]

#define STRINGIFY(x) #x
#define SENDVARIABLES(x,arg1,arg2,arg3,arg4,arg5,arg6)	\
do{	\
if(arg1!=NOTAVAR)	\
	addVariableDouble(x*6,arg1,(char*)"Player 2 " STRINGIFY(arg1) " " STRINGIFY(x));\
...
Re: preproccessor question
November 04, 2010 02:31PM
Thanks, that was the problem. But I have another question. Righ now if I don't have a total of six variables then I set the variables with that I don't want to use with a constant. So NOTAVAR is a number, which I set as 1111111111. There has to be a better way to detected if a value is a variable or not. Is there?
Re: preproccessor question
November 04, 2010 04:34PM
Well, looking at the limited amount of code you have posted. I think you might be looking for "var args": [en.wikipedia.org]
Which can be a bit troublesome to use (because you don't know how many arguments you got)

Maybe you want to post a few more details about what you are trying to accomplish? There might be a better way but more context helps.
Re: preproccessor question
November 05, 2010 06:14AM
Ok, sorry i was unclear about my intentions. I have made a simple library that sends a variable and a string idetifier to my computer. My computer recieves this message and saves it into a file so that I have a list of a variables changes throughout the life of the program. I add a new entry to this by calling the function addVariableTYPE(var number,variable value,str indetifier) where TYPE is the type of variable like int, float, ptr, string, char. This is then sent by another function to my computer. What I am trying to accomplish with the code I posted above is an easy way to add variables by only using their name. This way I wouldn't have to enter a custom string for each entry, but the other purpose is that since this is a debugging library, i want the calls to take up as few lines as possible. My goal for the code is to create a one line function that will take as many variable names as possible and turn them into addVaraibleTYPE() functions. The method that I was using before was defining a value, which happened to be 1111111111 that I could use to test if the value was a real variable or not. This way if I didn't have all six variables to fill the arguments of the function, then I could use this value in there place and know that they wouldn't be printed. I could post more code, but I'm not sure if it is nesscary.
I looked at the link you provided and I think that may be what I am looking for, but it doesn't seem like it would work for preproccessor code. Not that it has to be preproccessor code.
Sorry, only registered users may post in this forum.

Click here to login