C macro gives compile time error -


i want use macro expand function. wrote following code:

#define init ( t ) \     struct t * init##t() { \     struct t * obj  = ( struct t *)malloc( sizeof (struct t )); \     return obj; \ } \ 

i call macro using following :

init (mystruct); 

error ::

error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘struct’
warning: data definition has no type or storage class [enabled default]

i want write generalized macro accepts structure, allocates space object of structure , returns value same.

the grammar definition of function-like macros in 6.10 (1) says:

# define identifier lparen identifier-listopt ) replacement-list new-line

lparen: ( character not preceded white-space

there must not whitespace between macro name , opening parenthesis in macro definition (there may whitespace between them in macro invocations, however).

thus not define function-like macro object-like macro, expanding to

( t ) struct t * ... 

remove space:

#define init( t ) \     struct t * init##t() { \     struct t * obj  = ( struct t *)malloc( sizeof (struct t )); \     return obj; \ } 

and work.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -