Re: pointer to void property
- From: Irrwahn Grausewitz <irrwahn35@xxxxxxxxxx>
- Date: Sun, 11 Sep 2005 12:11:00 +0200
"aegis" <aegis@xxxxxxxxxxxxxxxxx> wrote:
>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?
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.
Best regards.
pointer.
--
Irrwahn Grausewitz (irrwahn35@xxxxxxxxxx)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
.
- Follow-Ups:
- Re: pointer to void property
- From: aegis
- Re: pointer to void property
- From: Jack Klein
- Re: pointer to void property
- References:
- pointer to void property
- From: aegis
- pointer to void property
- Prev by Date: Re: pointer to void property
- Next by Date: Re: pointer to void property
- Previous by thread: Re: pointer to void property
- Next by thread: Re: pointer to void property
- Index(es):
Relevant Pages
|
|