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
  • (*((volatile char xdata *)( CHIP_BASE + i)))
    ((volatile char xdata *)( CHIP_BASE + PTR_BASE + PTR_SIZE * i))

    There are two major differences. First one of them is an object in an array (which you would usually just write as
    array[i]
    ), the other is a pointer to an object in an array (written as
    &array[i]
    by newbies, or just
    array + i
    by more experienced people). In other words, they differ by one level of referencing.

    The second difference is that the layout of those two arrays is rather different. One is accessed as if it actually was what the pointer type in the cast says it is: an array of chars; the other is accessed like an array of things called "PTR"s, of size "PTR_SIZE". The second one is thus likely a bad idea of doing whatever it's supposed to do.

Reply
  • (*((volatile char xdata *)( CHIP_BASE + i)))
    ((volatile char xdata *)( CHIP_BASE + PTR_BASE + PTR_SIZE * i))

    There are two major differences. First one of them is an object in an array (which you would usually just write as
    array[i]
    ), the other is a pointer to an object in an array (written as
    &array[i]
    by newbies, or just
    array + i
    by more experienced people). In other words, they differ by one level of referencing.

    The second difference is that the layout of those two arrays is rather different. One is accessed as if it actually was what the pointer type in the cast says it is: an array of chars; the other is accessed like an array of things called "PTR"s, of size "PTR_SIZE". The second one is thus likely a bad idea of doing whatever it's supposed to do.

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