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

*((int *)&Buf[2]) = 0x12345678;

RVMDK v3.00a, Hello World sample for RealView compiler

#pragma pack(1)

unsigned char Buf[40];

int main (void)
{
*((short int *)&Buf[0]) = 0x77ff;
*((int *)&Buf[2]) = 0x12345678;
/* initialize the serial interface */
PINSEL0 = 0x00050000;
U1LCR = 0x83;
U1DLL = 97;
U1LCR = 0x03;

printf ("Hello World\n");
printf("%x %x %x %x \n", Buf[0],Buf[1],Buf[2],Buf[3]);
}

Hello World

78 56 34 12
------------
The same thing is for CARM compiler 2.4, 2.5

What I am wrong ?

Parents
  • 4) You cast the pointer to a pointer to unsigned char and perfectly legitimately access the bytes of the object by dereferencing that pointer.

    Covered by the "unportable" case --- because the result is implementation-defined. It's essentially impossible that the bytes of the object, as stored by a particular C implementation, can be used portably.

    Consider a 'C' implementation of the function scanf() as an example where pointer casting is portable, useful and correct.

    There's no need to explicitly cast pointers to call or implement scanf(). All the necessary work can be done by implicit casts (e.g. those hidden in an assignment). Not even to mention that an implementation of scanf() cannot actually be portable.

Reply
  • 4) You cast the pointer to a pointer to unsigned char and perfectly legitimately access the bytes of the object by dereferencing that pointer.

    Covered by the "unportable" case --- because the result is implementation-defined. It's essentially impossible that the bytes of the object, as stored by a particular C implementation, can be used portably.

    Consider a 'C' implementation of the function scanf() as an example where pointer casting is portable, useful and correct.

    There's no need to explicitly cast pointers to call or implement scanf(). All the necessary work can be done by implicit casts (e.g. those hidden in an assignment). Not even to mention that an implementation of scanf() cannot actually be portable.

Children
  • "Covered by the "unportable" case --- because the result is implementation-defined."

    The result of dereferencing the pointer is the byte stored at the address pointed to. This is perfectly portable.

    "It's essentially impossible that the bytes of the object, as stored by a particular C implementation, can be used portably."

    On the contrary - there are many situations where bytes are manipulated without any knowledge of their content. Take memcpy() as an example.

    "There's no need to explicitly cast pointers to call or implement scanf()."

    No need, but it's a perfectly reasonable approach.

    "Not even to mention that an implementation of scanf() cannot actually be portable."

    Why do you think not?