We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I want to know the differences between the following two definiations:
(*((volatile char xdata *)( CHIP_BASE + i))) ((volatile char xdata *)( CHIP_BASE + PTR_BASE + PTR_SIZE * i))
array[i]
&array[i]
array + i
written as &array[i] by newbies, or just array + i by more experienced people Ah, an irresistable spot to quibble. I don't think the syntax depends on experience. I'll write such an expression either way, depending on my needs. I often prefer the former when the rest of the local code deals with array subscripts, for the sake of consistency in syntax, or simply to emphasis the fact that this object is, in fact, an array (and thus somewhere there is a block of contiguous storage allocated, it has a definite size, and so on, rather than being a random address in the middle of nowhere). The second form tends to show up when I'm actually doing explicit address arithmetic; say, calculating a register offset from the base address of some peripheral. I also find myself using it a lot with a constant in expressions such as (ptrToPacketHeaderStruct + 1) in code that parses messages, as an idiom for "start of stuff past this header". It's cleaner to me than the explicit equivalent (((U8*)ptrToPacketHeaderStruct) + sizeof(PacketHeaderStruct)), but it does rely on the reader remembering that the "+ 1" is not one byte or word, but one sizeof-thing-pointed-to.