473,748 Members | 3,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2511
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.scie ntist.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*******@free net.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.c om> wrote:

Please preserve some context when posting replies. Context restored:
"aegis" <ae***@mad.scie ntist.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*******@free net.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*******@free net.de> wrote in comp.lang.c:
"aegis" <ae***@mad.scie ntist.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #6

Irrwahn Grausewitz wrote:
"aegis" <ae***@mad.scie ntist.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*******@spam cop.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*******@free net.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.scie ntist.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*******@free net.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
2733
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
2226
by: Neo | last post by:
Hi Folks, #include<stdio.h> int main() { int (*p); int arr; int i;
204
13059
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 = {0,1,2,4,9};
2
1762
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 a component when another component is destroyed ? I have a managed component called P. Let us say that C is another managed component. If on P I have: __property C * get_CComp(); __property void set_CComp(C *);
13
2996
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 sizeof(T). (Proof: In `T array;' both `array' and `array' must be correctly aligned.) Since `sizeof(unsigned char)' is divisible only by one and since one is a divisor of every type's size, every type is suitably aligned for `unsigned char'."
24
2471
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
1853
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 "that's what you get for lying". Can the behavior demonstrated be explained in standardese? Yes, I know the exact result is "undefined behavior". I can see what happened. But what was the actual violation? // Miles are converted to kilometers....
1
1359
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 to cv T? to a ?pointer to cv void? points to the start of the storage location where the object of type T resides, as if the object is a most derived object (1.8) of type T (that is, not a base class subobject)." Is it saying that the original...
12
3296
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, is it possible to convert a pointer into an integer, and then later (but on the same execution environment, ie the program has not exited, thus it's the same architecture, same compiler, same binary representations and so on) retrieve from the
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.