开发者

What does this macro define?

开发者 https://www.devze.com 2022-12-20 20:54 出处:网络
I read this piece of macro(C code) and was confused in decoding it to know what it defines. What does it define?

I read this piece of macro(C code) and was confused in decoding it to know what it defines. What does it define?

#defin开发者_运维问答e sram (*((unsigned char (*)[1]) 0))

-AD


I think sram means "start of RAM".


unsigned char[1]

An array of size 1 of unsigned chars.

unsigned char(*)[1]

A pointer to an array of size 1 of unsigned chars.

(unsigned char (*)[1]) 0

Cast 0 to a pointer to an array of size 1 of unsigned chars.

*((unsigned char (*)[1]) 0)

Read some memory at location 0, and interpret the result as an array of size 1 of unsigned chars.

(*((unsigned char (*)[1]) 0))

Just to avoid 1+5*8+1==42.

#define sram (*((unsigned char (*)[1]) 0))

Define the variable sram to the memory starting at location 0, and interpret the result as an array of size 1 of unsigned chars.


I think it's returning the base address (0) of memory (RAM) :)


It defines "sram" as a pointer to memory starting at zero. You can access memory via the pointer, e.g. sram[0] is address zero, sram[1] is the stuff at address one, etc.

Specifically it is casting 0 to be a pointer to an array of unsigned char and going indirect through that (leaving an array of unsigned char).

A similar result can be obtained by

#define sram ((unsigned char*)0)

It is also completely undefined in standard C, but that doesn't stop people from using it and having angels fly out of their navels.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号