This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Want to know the difference

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))

Thank you.

Parents
  • 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.

Reply
  • 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.

Children
No data