Arrays as macro

H

Henrik Goldman

Hello,

Lets say one wants to create an array of unsigned chars which would be
stored and used as a macro.
What syntax options do I have to do this?

E.g. if I have a string I would do:

#define MY_STRING "test"

However I cannot declare {1, 2, 3, 4} without variable names.
If I do

#define MY_ARRAY "\x01\x02\x03\x04"
then sizeof won't report 4 but 5.

Is there anything I'm missing or is it not possible?

Thanks.

-- Henrik
 
V

Victor Bazarov

Henrik said:
Lets say one wants to create an array of unsigned chars which would be
stored and used as a macro.
What syntax options do I have to do this?

E.g. if I have a string I would do:

#define MY_STRING "test"

However I cannot declare {1, 2, 3, 4} without variable names.

No, you cannot. It's called a compound initialiser and it is only
allowed in a declaration statements, to initialise an array or
an object of POD type.
If I do

#define MY_ARRAY "\x01\x02\x03\x04"
then sizeof won't report 4 but 5.

Correct. String literals have an extra element - the terminating
null character.
Is there anything I'm missing or is it not possible?

There are no "array literals" in C++ yet. What exactly are you trying
to accomplish?

V
 

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
473,777
Messages
2,569,604
Members
45,219
Latest member
KristieKoh

Latest Threads

Top