简体   繁体   中英

C++ Accessing define macros data

I have a C++ header file that has a definition like this...

#define TEMP {0x04,0x06,0xAA,0xBF ...}

How do I access that data in my C++ program?

For example? Let's say I wanted to capture the single byte?

How can I assign a variable of type uint8_t named x to the third byte in the TEMP structure?

uint8_t x = TEMP[2]; ?

Macro is simply pasted in source code on compilation.

So, you can do something like this:

char tmp[] = TEMP; // which expands to 'char tmp[] = {0x04, 0x06, ...}'

and then you work with it, like with usual array.

keep an array like this

uint8_t temp[] = TEMP;

which will expand to

uint8_t temp = {0x04,0x06,0xAA,0xBF};

then you can access the third value like this

uint8_t x = temp[2];

uint8_t x = std::vector<uint8_t>(TEMP)[2]; works but inelegantly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM