Re: 16 bit pointer typecast on 16 bit system



michaelquinlivan@xxxxxxxxx wrote:

yes, you should get 0x102 (if it is Big-endian) or 0x201 (if it is
Little-endian) but definitely not 0x1.

I've confirned this with the following test program in gcc:

#include <stdio.h>
char a[] = {0,1,2,3,4};
short b;
int main()
{
b = *(short *)(a+1);
printf("b = 0x%x\n",b);
return 0;
}

It prints the result "b = 0x201" (on an x86, which is little-endian).
I'd say that your compiler is broken...

I'd say your expectations are broken.

You take a value that isn't and never has been pointer-to-short, forcibly
convert it to pointer-to-short, and dereference it -- that sounds to me
like it would generate undefined behaviour.

--
Chris "seeker" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

.



Relevant Pages

  • CPU spikes when porting proprietary code from AS 2.1 to AS 3.0
    ... The test program was compiled with gcc version 3.2.3, ... The test code is as follows: ... int checkFile ...
    (RedHat)
  • Re: 16 bit pointer typecast on 16 bit system
    ... Little-endian) but definitely not 0x1. ... I've confirned this with the following test program in gcc: ... int main ...
    (comp.lang.c)
  • Re: data types
    ... Usually short is smaller than an int. ... Your compiler is either *really* old or broken or both. ... recommend either a version of gcc,, or Visual Studio ... Microsoft's Visual Studio Express has an onerous EULA). ...
    (comp.lang.c)
  • Re: Why doesnt std::cin choke on this?
    ... when I was writing a user-driven test program for a data structure I ... You read an int. ... it simpler then checking the stream for it's ... read in a string, this becomes trivial. ...
    (comp.lang.cpp)
  • Re: stream io in c
    ... zero to 255, putchar'ing in the body of the loop, then redirecting output ... int main ... // gcc -o chars mkchars1.c ... This compiles but gcc warns of incompatible pointer types. ...
    (comp.lang.c)