Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!husc6!ut-sally!ghostwheel!milano!im4u!jai From: jai@im4u.UUCP (Jai Srinivasan) Newsgroups: comp.sys.ibm.pc,comp.lang.c Subject: Re: Assigning to Pointers Message-ID: <1823@im4u.UUCP> Date: Sun, 10-May-87 14:55:38 EDT Article-I.D.: im4u.1823 Posted: Sun May 10 14:55:38 1987 Date-Received: Mon, 11-May-87 03:07:26 EDT References: <3537@vrdxhq.UUCP> <857@killer.UUCP> Reply-To: jai@im4u.UUCP (Jai Srinivasan) Organization: U. Texas CS Dept., Austin, Texas Lines: 41 Xref: mnetor comp.sys.ibm.pc:3931 comp.lang.c:2131 In article <857@killer.UUCP>, John Haugh (jfh@killer.UUCP) writes: > The parenthesis come in real handy at times, for example (small lesson in > C to follow :-) when you can't figure out how to declare a 'something' with > a weird shape. My weakness is pointers to functions - > int foo (); /* this is a function */ > int *foo (); /* this is a function RETURNING a pointer */ > int *(foo ()); /* pointer to first function */ int *foo(); and int *(foo()); are equivalent because the () has a higher precedence than the * in the declaration. To make foo a pointer to a function which returns an integer, the declaration ought to be int (*foo)();. I guess the same holds with the array declarations stated earlier in that article because [] (like ()) has a higher precedence than the *. Reading these declarations is not all that hard: just use the precedence rules. The parentheses around the * in int (*foo)(); indicates that the * should be interpreted before the (), and reading "*" as "pointer to" and "()" as "function that returns", one reads int *foo(); as "foo is a function that returns a pointer to an integer", and int (*foo)(); as "foo is a pointer to a function that returns an integer". Similarly, int (*signal())(); is read as "signal is a function that returns a pointer to a function which returns an integer". I don't know if K&R ever explicitly state that the precedence rules hold in the declaraions as well, but I assume they do. Jai. ----------------- Jai Srinivasan, UUCP: {gatech,harvard,ihnp4,pyramid,seismo}!ut-sally!im4u!jai ARPA: jai@im4u.UTEXAS.EDU, jai@sally.UTEXAS.EDU