By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,034 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,034 IT Pros & Developers. It's quick & easy.

pointer to void property

P: n/a
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?
--
aegis

Nov 15 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
The pointer value will be the same, but you must dereference it in the
correct way to get the expected result.
If you perform:
*p2, you will get the first byte of the a variable read as a uchar.
That's usefull when you want to see the internal representation of
individuals bytes.

Also remember to cast from void to a type in order to avoid warnings:
p2 = (unsigned char *) pl;

Nov 15 '05 #2

P: n/a
"aegis" <ae***@mad.scientist.com> 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 (ir*******@freenet.de)
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
Nov 15 '05 #3

P: n/a
"Nerox" <ne****@gmail.com> wrote:

Please preserve some context when posting replies. Context restored:
"aegis" <ae***@mad.scientist.com> 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'?

The pointer value will be the same, but you must dereference it in the
correct way to get the expected result.
If you perform:
*p2, you will get the first byte of the a variable read as a uchar.
That's usefull when you want to see the internal representation of
individuals bytes.

Also remember to cast from void to a type in order to avoid warnings:
p2 = (unsigned char *) pl;


Uck, no, don't. Implicit conversion to and from void is perfectly
valid, as opposed to conversions between pointers to different types
other than void. After all, that's one of the reasons why generic
pointers were introduced in C, IIRC.

Best regards.
--
Irrwahn Grausewitz (ir*******@freenet.de)
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
Nov 15 '05 #4

P: n/a
Nerox wrote on 11/09/05 :
Also remember to cast from void to a type in order to avoid warnings:
p2 = (unsigned char *) pl;


Assuming p2 has the type 'void *' and as long as you are using a
C-compiler, there is no need for that.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
Nov 15 '05 #5

P: n/a
On Sun, 11 Sep 2005 12:11:00 +0200, Irrwahn Grausewitz
<ir*******@freenet.de> wrote in comp.lang.c:
"aegis" <ae***@mad.scientist.com> 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.


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
Nov 15 '05 #6

P: n/a

Irrwahn Grausewitz wrote:
"aegis" <ae***@mad.scientist.com> 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.


I wasn't asking about pointer-to-int to pointer-to-double
nor about pointer-to-sometype to pointer-to-void but specifically about
pointer-to-sometype to pointer-to-void to
pointer-to-[signed|unsigned]char
With something involving pointer-to-int to pointer-to-void to
pointer-to-double, the result from what I can see, may not be correctly
aligned.

--
aegis

Nov 15 '05 #7

P: n/a
aegis wrote:

Irrwahn Grausewitz wrote:
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.

I wasn't asking about pointer-to-int to pointer-to-double
nor about pointer-to-sometype to pointer-to-void
but specifically about
pointer-to-sometype to pointer-to-void to
pointer-to-[signed|unsigned]char


Pointer to an object to pointer-to-[signed|unsigned]char,
is defined.

--
pete
Nov 15 '05 #8

P: n/a
Jack Klein <ja*******@spamcop.net> wrote:
On Sun, 11 Sep 2005 12:11:00 +0200, Irrwahn Grausewitz

<snip>
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.


Or even better, just assigning and allowing the automatic conversion
to happen. No casting required.


I meant to say conversion, not casting. You're of course right.
--
Irrwahn Grausewitz (ir*******@freenet.de)
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
Nov 15 '05 #9

P: n/a
"aegis" <ae***@mad.scientist.com> wrote:

Irrwahn Grausewitz wrote: <snip>
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.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

s/casting/conversion/
I wasn't asking about pointer-to-int to pointer-to-double
nor about pointer-to-sometype to pointer-to-void but specifically about
pointer-to-sometype to pointer-to-void to
pointer-to-[signed|unsigned]char


If it's safe to convert a p-to-sometype to p-to-void, AND it's safe
to convert a p-to-void to p-to-char, THEN it's safe to convert a
p-to-int to p-to-void to p-to-char.

What a typing mess...

Best regards.
--
Irrwahn Grausewitz (ir*******@freenet.de)
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
Nov 15 '05 #10

P: n/a
Irrwahn Grausewitz wrote:

If it's safe to convert a p-to-sometype to p-to-void, AND it's safe
to convert a p-to-void to p-to-char, THEN it's safe to convert a
p-to-int to p-to-void to p-to-char.

What a typing mess...


The OP's original question was (rephrased):

Can this p-to-char be used to access the representation of the
"sometype" that p-to-sometype was pointing to?

which doesn't seem to have been answered yet on this thread
(everyone was distracted by the casting issue). I'm not sure
of the answer personally.

Nov 15 '05 #11

P: n/a
"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Irrwahn Grausewitz wrote:

If it's safe to convert a p-to-sometype to p-to-void, AND it's safe
to convert a p-to-void to p-to-char, THEN it's safe to convert a
p-to-int to p-to-void to p-to-char.

What a typing mess...


The OP's original question was (rephrased):

Can this p-to-char be used to access the representation of the
"sometype" that p-to-sometype was pointing to?

which doesn't seem to have been answered yet on this thread
(everyone was distracted by the casting issue). I'm not sure
of the answer personally.


Well, even if you do so, you would have to account for (ie: guess) byte
alignments that might change between compiler versions, or CPU changes
(32bit vs 64bit currently), or OS changes (versions), or OS changes
(endian issues), or OS changes (devices like PDAs), or OS changes (you
get the idea).

--
Mabden
Nov 15 '05 #12

P: n/a
"Old Wolf" <ol*****@inspire.net.nz> wrote:
Irrwahn Grausewitz wrote:

If it's safe to convert a p-to-sometype to p-to-void, AND it's safe
to convert a p-to-void to p-to-char, THEN it's safe to convert a
p-to-int to p-to-void to p-to-char.

What a typing mess...


The OP's original question was (rephrased):

Can this p-to-char be used to access the representation of the
"sometype" that p-to-sometype was pointing to?

<snip>

Of course. A pointer-to-charactertype can be used to access the
individual bytes of any object that occupies the memory location
said pointer happens to point to (Ref: C99 6.3.2.3p7).

IIRC OP's question was, if this is still valid when the p-to-char
was not converted directly from the pointer to the original object,
but "went through" a pointer-to-void. It is (Ref: C99 6.2.5p26,
6.3.2.3p1, 6.3.2.3p7).

Aside: in some sense, a pointer-to-void is just a pointer-to-char
in disguise: it shall, as the authors of the standard put it, have
the same representation and alignment requirements.

Best regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
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
Nov 15 '05 #13

P: n/a
"Mabden" <mabden@sbc_global.net> wrote:
"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11**********************@g47g2000cwa.googleg roups.com... <snip>
The OP's original question was (rephrased):

Can this p-to-char be used to access the representation of the
"sometype" that p-to-sometype was pointing to?

<snip>Well, even if you do so, you would have to account for (ie: guess) byte
alignments that might change between compiler versions, or CPU changes
(32bit vs 64bit currently), or OS changes (versions), or OS changes
(endian issues), or OS changes (devices like PDAs), or OS changes (you
get the idea).


If a C programmer messes around with the internal representation of
objects, he knows he's left the realm of portable C. Well, at least
he /should/ know. ;-)

Best regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
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
Nov 15 '05 #14

P: n/a
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
Of course. A pointer-to-charactertype can be used to access the ^^^^^^^^^^^^^^^^^^^^^^^^

Only if the type in question is unsigned.
individual bytes of any object that occupies the memory location
said pointer happens to point to (Ref: C99 6.3.2.3p7).


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #15

P: n/a
Christopher Benson-Manica wrote:
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
Of course. A pointer-to-charactertype can be used to access the

^^^^^^^^^^^^^^^^^^^^^^^^

Only if the type in question is unsigned.


Did you read 6.3.2.3p7?
individual bytes of any object that occupies the memory location
said pointer happens to point to (Ref: C99 6.3.2.3p7).


...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.

Of course, only unsigned chars are guaranteed to be able to
differentiate
all byte values.

--
Peter

Nov 15 '05 #16

P: n/a
"Peter Nilsson" <ai***@acay.com.au> wrote:
Christopher Benson-Manica wrote:
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
> Of course. A pointer-to-charactertype can be used to access the

^^^^^^^^^^^^^^^^^^^^^^^^

Only if the type in question is unsigned.


Did you read 6.3.2.3p7?
> individual bytes of any object that occupies the memory location
> said pointer happens to point to (Ref: C99 6.3.2.3p7).


...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.

Of course, only unsigned chars are guaranteed to be able to
differentiate
all byte values.


I guess I used the word "access" in a sloppy fashion. Sorry for
causing confusion.

Best regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
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
Nov 15 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.