Mail Archives: djgpp/2000/05/19/03:15:36
"A. Sinan Unur" wrote:
>
> > "A. Sinan Unur" wrote:
> > > what do you expect the following code to produce:
> > >
> > > #include <stdio.h>
> > >
> > > int main(void) {
> > > char c;
> > > unsigned int i2;
> > > unsigned int i1 = 0x12345678;
> > >
> > > c = (char) i1;
> >
>
> "Alexei A. Frounze" <alex DOT fru AT mtu-net DOT ru> wrote in message
> news:<39242D85 DOT 6A95D430 AT mtu-net DOT ru>...
>
> > i1 exceeds valid range. Actually I mean values that can be represented
> > by both "<unsigned>int" and "size_t".
>
> i1 is a 32-bit unsigned interger. how does it exceed the values
> representable by unsigned int?
That "unsigned int" exceeds valid range for "char". 8-bit "char" is unable
to fit 32-bit "unsigned int".
Don't ask me such questions anymore, 'coz I know math and how numbers are
represented in a computer very well. :))
> > > i2 = (unsigned int) c;
> >
> > Don't overact.
> >
> > >
> > > printf("i1: %8.8x\tc: %2.2x\ti2: %8.8x\n", i1, c, i2);
> > >
> > > return 0;
> > > }
> > >
> > > do you think i1 == i2?
> >
> > Am I that stupid???
>
> that wasn't what i was trying to say. all i am trying to say is this :
Really? If so, Okay.
>
> the problem with casting an object of type size_t to int is the same as the
> problem with casting an unsigned int to char. sure enough there may be
> platforms where you don't loose any information in the process, but it is
> not safe. what if size_t is 64 bits and int is 32 bits? casting how can
> casting size_t to int be safe (even disregarding the sign issue, which is
> bad enough).
Suppose we have a string. If the string is definetely shorter than INT_MAX
and SSIZE_MAX (say less than 32000 characters), there must be everything
okay with type casting for strlen(), strncpy(), etc.
--
Alexei A. Frounze
-----------------------------------------
Homepage: http://alexfru.chat.ru
Mirror: http://members.xoom.com/alexfru
- Raw text -