mod.std.c Digest V6#7
Orlando Sotomayor-Diaz
osd7 at homxa.UUCP
Sun May 12 00:52:56 AEST 1985
From: Orlando Sotomayor-Diaz (The Moderator) <cbosgd!std-c>
mod.std.c Digest Sat, 11 May 85 Volume 6 : Issue 7
Today's Topics:
Pointer arithmetic and function pointers (4 msgs)
----------------------------------------------------------------------
Date: Fri, 10 May 85 10:28:01 pdt
From: John Bruner <seismo!mordor!jdb>
Subject: Pointer arithmetic and function pointers
To: std-c at cbosgd
> No! No! No! What about the following (which is legal and works):
>
> int (*afp[])();
>
> This is an array of pointers to functions. All arrays in C can be
> used as xxx[n] or *(xxx+n); more explicitly, an array name is actually
> the address of the first element of the array and is a constant (which
> is why an array name cannot be an lvalue). Put this all together and
> you get that arithmetic on a pointer-to-function is a necessary thing.
Given the array of pointers to functions
int (*afp[])();
the semantically equivalent expressions
afp[n] *(afp+n)
(where "n" is an integer expression) are not performing arithmetic on
pointers to functions. The arithmetic is performed on a pointer to a
pointer to function. The type of "afp" in an expression (except sizeof)
is (int (**)()) [C.2.2.1]. The integer expression is scaled by
"sizeof (int (*)())", making this equivalent to
(int (*)()) ( (unsigned long)afp + n*sizeof(int (*)()) )
--
John Bruner (S-1 Project, Lawrence Livermore National Laboratory)
MILNET: jdb at mordor.ARPA [jdb at s1-c] (415) 422-0758
UUCP: ...!ucbvax!dual!mordor!jdb ...!decvax!decwrl!mordor!jdb
------------------------------
Date: Fri, 10 May 85 14:39:38 edt
From: Alex Dupuy <seismo!columbia!dupuy>
Subject: Pointer arithmetic and function pointers
To: std-c at cbosgd
>>Since the size of a function object is not defined, I believe this
>>should be amended to disallow expressions which combine integers and
>>pointers to functions:
>>
>> foo + 1 /* illegal */
>
>No! No! No! What about the following (which is legal and works):
>
> int (*afp[])();
>
>This is an array of pointers to functions.
-----------
Exactly. Since it is an array of function -pointers-, when you do something
like
*afp[1] (arg);
or equivalently, *(*(afp+1)) (arg);
you're adding 1 to a (constant) pointer to a -pointer to a function-. Though
"sizeof int()" is undefined, "sizeof int *()" is defined, usually the same as
sizeof(*int). Since the first message doesn't say anything about pointers to
pointers to functions, your construct is perfectly legal. The twisted nature
of C type declarations and the array/pointer distinction make this a confusing
problem, but there is no reason to allow adding integers to function pointers,
except that it might cause others to make the same mistake as you. One could
make a case for multiple entry points to a function, though; allowing you to
add a -label- to a function, but this is an extension to C which doesn't really
belong in the standard.
@alex
------------------------------
Date: 10 May 85 13:19:28 CDT (Fri)
From: ihnp4!bbncca!keesan
Subject: Pointer arithmetic and function pointers
To: ihnp4!cbosgd!std-c, ihnp4!nbires!gangue!obo586!dir
Arithmetic on pointers to pointers is fine, but I agree with the
original poster that arithmetic on pointers to functions should be
explicitly prohibited.
Morris M. Keesan
{decvax,linus,ihnp4,wanginst,wjh12,ima}!bbncca!keesan
keesan @ BBN-UNIX.ARPA
------------------------------
Date: Fri, 10 May 85 13:07:06 CDT
From: William LeFebvre <ucbvax!phil at rice.ARPA>
Subject: Pointer arithmetic and function pointers
To: cbosgd!nbires!gangue!obo586!dir at BERKELEY
I agree with the original posting: arithmetic on a pointer-to-function
makes no sense.
William LeFebvre
Department of Computer Science
Rice University
<phil at Rice.arpa>
------------------------------
End of mod.std.c Digest - Sat, 11 May 85 10:50:39 EDT
******************************
USENET -> posting only through cbosgd!std-c.
ARPA -> ... through cbosgd!std-c at BERKELEY.ARPA (NOT to INFO-C)
In all cases, you may also reply to the author(s) above.
More information about the Mod.std.c
mailing list