Article View: comp.std.c
Article #6156Re: Void pointers and pointer arithmetic
From: Brian T. Schellenberger
Date: Sat, 06 Jan 1990 18:45
Date: Sat, 06 Jan 1990 18:45
45 lines
2143 bytes
2143 bytes
In article <1990Jan3.073624.14061@twwells.com> bill@twwells.com (T. William Wells) writes:
|In article <1055@esatst.yc.estec.nl> arne@yc.estec.nl (Arne Lundberg) writes:
|:
|: I am trying to write some code that tries to access members in a structure
|: by knowing the start address and the offset to a particular member element.
|: The following program shows a small example (the real program does not
|: hardcode the offsets etc.) Is this program legal in ANSI C, will it
|: produce the desired result?
|:
|: The (Non-ANSI) compilers I have tested either gives the value for `a'
|: three times or complains about ``unknown size for pointer to void''.
|: It works perfectly well if I change the type of p to be ``char *''.
|:
|: ---------------------------------------
|: struct x {
|: int a, b, c;
|: } x = {
|: 1, 2, 3
|: };
|:
|: main()
|: {
|: void *p = &x;
|: printf("a %d\n", *(int *)(p + 0));
|: printf("b %d\n", *(int *)(p + 4));
|: printf("c %d\n", *(int *)(p + 8));
|: }
|: ---------------------------------------
. . . so just use "char *"; this works under ANSI, too.
1. offsetof is the size in bytes. (4.1.5)
2. sizeof yields the sie in bytes;
When applied to char . . . the result is 1. (3.3.3.4)
3. A pointer to void shall htave the representation and
alignment requirements as a pointer to character type (3.1.2.5,
the paragraph just before examples).
Ergo, this must work under ANSI with char *.
--
-- Brian, the Man from Babble-on. ...!mcnc!rti!sas!bts
-- (Brian Schellenberger)
"No one will ever write a song called 'Nitro Burning Funny Cars'"
-- THE DEAD MILKMEN, "Nitro Burning Funny Cars"
Technical Details
Message-ID:
<1421@sas.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!mcnc!rti!sas!btsReferences:
<1055@esatst.yc.estec.nl> <1990Jan3.073624.14061@twwells.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!mcnc!rti!sas!bts From: bts@sas.UUCP (Brian T. Schellenberger) Newsgroups: comp.std.c Subject: Re: Void pointers and pointer arithmetic Message-ID: <1421@sas.UUCP> Date: 6 Jan 90 18:45:24 GMT References: <1055@esatst.yc.estec.nl> <1990Jan3.073624.14061@twwells.com> Reply-To: bts@sas.UUCP (Brian T. Schellenberger) Organization: SAS Institute Inc, Cary NC Lines: 45