Casting pointers

Alvin E. Sylvain asylvain at felix.UUCP
Fri Oct 5 04:58:28 AEST 1990


In article <1990Sep29.201144.23113 at maths.tcd.ie> tim at maths.tcd.ie (Timothy Murphy) writes:
>I recently came across the following problem,
>when temporarily 'borrowing' od from Unix to Mac.
>Consider a program like this.
>
>#include <stdio.h>
>char *buf = "abc";
>main()
>{
>  int *n = (int*) buf;
>  printf("%d", *n);
>}
>
>Should this always work?
>On the Mac, with THINK C,
>it only works if buf has an even address.
>Otherwise it bombs out.
>Is that a bug, according to standard C?

I don't know off-hand what the "right" answer is to this one,
but I personally lump this kind of problem into the following category:

Patient, demonstrating a painful maneuver: "Doc, it hurts when I do dat."

Doctor, repeating same maneuver:  "Don't do dat!"

Put another way, unless you can convince me you have compelling reasons
to cast a character pointer into an integer pointer, then treat it
like a character pointer, "don't do dat!"

Change the "int *n" to "char *n", and let it go at that.
--
=======================Standard Disclaimers Apply=======================
"We're sorry, but the reality you have dialed is no   |            Alvin
longer in service.  Please check the value of pi,     |   "the Chipmunk"
or pray to your local diety for assistance."          |          Sylvain



More information about the Comp.std.c mailing list