Article View: comp.std.c
Article #2180Re: struct hack, and other out-of-array references
From: T. William Wells
Date: Thu, 01 Oct 1992 10:15
Date: Thu, 01 Oct 1992 10:15
49 lines
2852 bytes
2852 bytes
In article <1992Oct1.070345.14854@sq.sq.com> msb@sq.sq.com (Mark Brader) writes:
: > Consider the question: what is (char*)&a[0][1] - (char *)&a[0][0]?
: > sizeof(&a[0][0]) of course. Carried out repeatedly, one sees that
: > each subelement of a is at the expected offset from the start of
: > a. The only question you might have is: what is (char*)&a[x][0] -
: > (char*)&a[x-1][y-1], where y is the number of elements in a[x].
: > The rules on sizeof and contiguity of objects require that to be
: > sizeof(&a[0][0]) as well.
:
: For ease of discussion I'll assume that a[0][0] has type int.
:
: The above is correct as far as it goes. And it implies that you can do
:
: int *p = &a[x-1][y-1];
: p = (int *) ((char *) p + 2 * sizeof (int));
:
: and *p will now be a[x][1]. But you _cannot_ replace the second
: statement with a simple "p += 2;" and expect the same result. This
: is the thrust of the Interpretation Ruling that was quoted earlier
: in the thread. You really have to make the excursion into char *
: and back: The standard requires that "a" be treatable as an array
: of bytes, and therefore as an array of any character type, but not
: as an array of int.
Sorry, but no. If you can treat it as an array of bytes, you can
treat it as an array of *anything*, provided that you don't
violate alignment restrictions. That's the point of the rules on
pointer casting and pointer arithmetic. (Alternately, if we were
to imagine that your comment was true, there isn't anything
special about the int* type; it would be equally illegal to index
past the subarray with char* pointers.)
: > In other words, these pointers are just pointers into an array,
: > and correspond in the expected way to pointers into the doubly
: > indexed array.
:
: They point to locations which are located as they would be in a
: simple array, but the permitted operations are not the same.
The standard certainly doesn't say that and, as far as I know
(not having the official interpretations, only Doug Gwyn's
comments), the interpretation talks about indexing past
subarrays, not about manipulating the converted pointers. If you
have the text of the interpretation ruling and it says otherwise,
I'll recant; otherwise, I see no reason to change my opinion.
---
Bill { rutgers | decwrl | telesci }!twwells!bill
bill@twwells.com
Technical Details
Message-ID:
<BvFttL.KHM@twwells.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!ogicse!decwrl!twwells!billReferences:
<1992Sep25.012337.22640@sq.sq.com> <BvDnHn.CMK@twwells.com> <1992Oct1.070345.14854@sq.sq.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!ogicse!decwrl!twwells!bill From: bill@twwells.com (T. William Wells) Newsgroups: comp.std.c Subject: Re: struct hack, and other out-of-array references Message-ID: <BvFttL.KHM@twwells.com> Date: 1 Oct 92 10:15:17 GMT Article-I.D.: twwells.BvFttL.KHM References: <1992Sep25.012337.22640@sq.sq.com> <BvDnHn.CMK@twwells.com> <1992Oct1.070345.14854@sq.sq.com> Organization: None, Mt. Laurel, NJ Lines: 49