We help IT Professionals succeed at work.

how can i pass #define to function as an argument

ofirg
ofirg asked
on
suppose i have
#define DEF1 {1,1}
#define DEF2 {2,2}

int func(x)
{  
  int *i = x ;
  return 0;
}

what should be x?
Comment
Watch Question

CERTIFIED EXPERT
Author of the Year 2009

Commented:
It is hard to tell from the question, but I think you want to know how you can pass a two element array to a function.

You need to allocate it:

   int anTwoElemArray[2]= DEF1; // {1,1}

   int nRet= func( anTwoElemArray );
or
   int nRet= func( &anTwoElemArray[0] );
or
   int* pan= anTwoElemArray; // point to first element
   int nRet= func( pan );

But the question is quite ambiguous and, candidly silly.  So why not describe what you really want to do?  

-- Dan

Author

Commented:
i want to pass the #define itself to function as an argument not by assign it to other variable first
somthing like

i = func( (int*) DEF1);

Commented:
The constans DEF1 and DEF2 are recognized all over the program , including inside the function.
Why do you want to pass it ?
Commented:
#define just says to the precompiler "replace this token with this other one", so it knows nothing about language.  The compiler knows nothing about the #defines.  So your example above is seen as

i = func((int*){1,1});

by the compiler.  That's why it doesn't work.  If you're trying to define constant arrays with certain values, why don't you tell the program that:

const int DEF1[] = {1,1};
.
.
i = func(DEF1);

will work fine.  Plus, it is type checked, it will obey scope etc.
CERTIFIED EXPERT
Author of the Year 2009

Commented:
But the question is quite ambiguous and, candidly silly.  So why not describe what you really want to do?  

-- Dan

Author

Commented:
thanx man.

const int DEF1[] = {1,1}; works and help me very much.