On Sun, 11 Sep 2005 12:11:00 +0200, Irrwahn Grausewitz
<irrwahn35@freenet.de> wrote in comp.lang.c:
[color=blue]
> "aegis" <aegis@mad.scientist.com> wrote:[color=green]
> >Given the following:
> >
> >int a = 10;
> >
> >int *p;
> >void *p1;
> >unsigned char *p2;
> >
> >p = &a;
> >
> >p1 = p;
> >
> >p2 = p1;
> >
> >is a guarantee made that I can access the object representation
> >via 'p2'?
> >
> >Will the value assigned between different pointer to object types
> >through use of a generic pointer preserve this value?
> >
> >I know it is guaranteed to be preserved if it is assigned from a
> >pointer to an object type to a void pointer and back again to said
> >pointer to object type. But will the value assigned between different
> >pointer to object types through use of a generic pointer, preserve this
> >value?[/color]
>
> As long as you stick with a character pointer, yes. Consider:
>
> ISO/IEC 9899:1999 (E)
>
> 6.2.5p26
> A pointer to void shall have the same representation and alignment
> requirements as a pointer to a character type.
>
> 6.3.2.3p1
> A pointer to void may be converted to or from a pointer to any
> incomplete or object type. [...]
>
> 6.3.2.3p7
> A pointer to an object or incomplete type may be converted to a
> pointer to a different object or incomplete type. If the resulting
> pointer is not correctly aligned for the pointed-to type, the
> behavior is undefined. Otherwise, when converted back again, the
> result shall compare equal to the original pointer. When a pointer
> to an object is converted to a pointer to a character type, the
> result points to the lowest addressed byte of the object. Successive
> increments of the result, up to the size of the object, yield
> pointers to the remaining bytes of the object.
>
> Note that (AFAICT) it's not guaranteed to work with, e.g., a
> pointer-to-int and a pointer-to-double. But casting from any
> pointer-to-sometype to pointer-to-void is safe, as is casting from any
> pointer-to-void to a pointer-to-charactertype.[/color]
Or even better, just assigning and allowing the automatic conversion
to happen. No casting required.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html