To dereference or not dereference, that is the question

Don E. Davis ded at aplvax.UUCP
Tue Mar 4 01:37:45 AEST 1986


In article <857 at pucc-j> aaz at pucc-j.UUCP (Marc Mengel) writes:
>
>char foo[BIGNUM];
>main()
>{
>        int result;
>
>	/* code to put machine code into foo[] */
>
>	result = (* (int (*)()) foo)();
>}
>-- 

An interesting corollary to this issue is the following.  It
doesn't seem to matter if I deference function pointers or not.
This seems wrong to me.  I feel the y(2) and ((int (*)())x)(3) function 
invocations should be illegal.

#include <stdio.h>
main(argc, argv)
int argc;
char **argv;
{
	int x;
	int (*y)();	/* y is pointer to function returning int */
	int func();

	y = func;	/* y points to func */
	(*y)(1);	/* dereference y and invoke func */
	y(2);		/* DON'T dereference y and invoke func! */

	/* the same as above with casting */
	x = (int)func;
	((int (*)())x)(3);
	(*(int (*)())x)(4);
	exit(0);
}
func(param)
int param;
{
	printf("hello %d\n", param);
}


All of the above appear to be equivalent.  What the heck is going on here?
-- 

					Don Davis
					JHU/APL
				...decvax!harpo!seismo!umcp-cs!aplcen!aplvax!ded
				...rlgvax!cvl!umcp-cs!aplcen!aplvax!ded



More information about the Comp.lang.c mailing list