macro for compile time allocation

M

moose

Can someone explain why the following will not work,

#define MYSTRUCT(name,size) struct my_struct_ ## name {
unsigned char name ## _array[ size ] ;
} ;

MYSTRUCT( foo , 10 )

The error is because the size doesnt appear to be known at compile time
to create the array but why not ? this is a macro not a function.

Any comments welcome
 
P

pete

moose said:
Can someone explain why the following will not work,

#define MYSTRUCT(name,size) struct my_struct_ ## name {
unsigned char name ## _array[ size ] ;
} ;

You need two backslashes.

#define MYSTRUCT(name,size) struct my_struct_ ## name {\
unsigned char name ## _array[ size ] ;\
} ;
 
C

CBFalconer

pete said:
moose said:
Can someone explain why the following will not work,

#define MYSTRUCT(name,size) struct my_struct_ ## name {
unsigned char name ## _array[ size ] ;
} ;

You need two backslashes.

#define MYSTRUCT(name,size) struct my_struct_ ## name {\
unsigned char name ## _array[ size ] ;\
} ;

You might tell him why. #defines have to be complete on one line.
To write them on multiple lines you have to escape the line
endings, which means an escape char (the \) at the end of each
continued line. Nothing follows the escape except the eoln marker.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top