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
  • The first one has an extra * in it. The pointer will be "dereferenced", and thus the first line refers to an actual object (a char, a volatile xdata char, to be more precise).

    The second lacks the *, and so this object is a pointer-to-char type, not a char.

    There are of course the differences in the address calculated, which should be trivial. If these two expressions are part of a macro with a parameter i, then you might run into trouble with the expanded version of "i". In #define macros, it's a good idea always to wrap the parameters in a layer of parenthesis to make sure the precedence is correct.

    #define CHIP_REG(i) (*((char *)(BASE + (i))))
    

Reply
  • The first one has an extra * in it. The pointer will be "dereferenced", and thus the first line refers to an actual object (a char, a volatile xdata char, to be more precise).

    The second lacks the *, and so this object is a pointer-to-char type, not a char.

    There are of course the differences in the address calculated, which should be trivial. If these two expressions are part of a macro with a parameter i, then you might run into trouble with the expanded version of "i". In #define macros, it's a good idea always to wrap the parameters in a layer of parenthesis to make sure the precedence is correct.

    #define CHIP_REG(i) (*((char *)(BASE + (i))))
    

Children
More questions in this forum