473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

pointer to void property

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
16 2476
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
"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
"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
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
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

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
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
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
"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
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
"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
"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
"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Leon Brodskiy | last post by:
Hi, Could please anyone clarify about pointer and array in C? If I have: int arr; The following two commands will be the same: arr and &arr.
22
by: Neo | last post by:
Hi Folks, #include<stdio.h> int main() { int (*p); int arr; int i;
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
2
by: Edward Diener | last post by:
How does one specify in a component that a property is a pointer to another component ? How is this different from a property that is actually an embedded component ? Finally how is one notified in...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
24
by: pinkfloydhomer | last post by:
Is it well-defined to make a cast from a pointer to an int and back? Like: typedef struct { int whatever; } S; int main(void) {
15
by: Steven T. Hatton | last post by:
I found the example code below, listed in the book described here: http://cartan.cas.suffolk.edu/moin/OopDocbookWiki The result was a bit surprising. I guess it falls into the category of...
1
by: Steven T. Hatton | last post by:
I simply don't get this one: § 4.9/2 "An rvalue of type ?pointer to cv T,? where T is an object type, can be converted to an rvalue of type ?pointer to cv void.? The result of converting a ?pointer...
12
by: lithiumcat | last post by:
Hi, I bothered you a while back about storing integer values in void*. Now in a completely unrelated context, I'm trying to store pointer values in an integer type. So the basic question is,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.