Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!samsung!ernie.viewlogic.com!news From: sparks@power.viewlogic.com (Alan Sparks) Newsgroups: comp.lang.c Subject: Re: declarations using pointers to functions Keywords: declaration, pointers, functions Message-ID: <1991Mar25.171853.4229@viewlogic.com> Date: 25 Mar 91 17:18:53 GMT References: <27514@rouge.usl.edu> Sender: news@viewlogic.com (News Administrator) Organization: Viewlogic Systems, Inc., Marlboro, MA Lines: 96 In article <27514@rouge.usl.edu> anon@rouge.usl.edu (Anonymous NNTP Posting) writes: >Hi, > >Can someone tell me the "spiralling" or "circular" convention > >used in the complex variable declarations involving array of > >pointers to functions. > >I heard that there is a standard convention to be followed... > >Thanx, > >Rajiv > >%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% >EMAIL: rxd0219@usl.edu >%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% When I used to teach C, I used a book entitled "Advanced C Tips and Techniques" by Gail and Paul Anderson in the second semester of the class. They documented a rule called the "right-left rule," which I'll try to condense here. [loosely copied, used without permission] Define the attribute/keyword relationship: Attribute English keyword () function returns [n] array of n * pointer to To decipher a declaration, substitute the English keyphrase where an attribute appears. Now the rule: 1. Start with the identifier. 2. Look to the right for an attribute. 3. If none found, look to the left. 4. If found, substitute the attribute. 5. Continue right-left substitution as you work outward. 6. Stop when you reach the data type in the declaration. Remember that multidimensional arrays use the [] attribute more than once; in this case, continue to the right until you run out of [] before you look left. Let's try a couple... struct something *ps[3][4]; ps is an array of 3 arrays of 4 pointers to struct something. int (*pf[5])() pf is an array of 5 pointers to function returning int. The rule can be used backwards also. Basically, substitute the attribute where the English keyphrase occurs in your statement of what you need (but see the caveat below): buf is array of ten pointers to function returning pointer to int. int *(*buf[10])(); Why the intervening parentheses? Certain combinations of attributes are illegal (not defined by C): []() functions cannot return arrays; only pointers to. []() arrays of functions; you CAN have arrays of function *pointers*, however. ()() functions can't return functions; you CAN return a *pointer* to a function, however. Parentheses must be used to keep the lower precedence operator (namely *) grouped with the correct higher precedence operator (like [] or ()). Therefore, int *foo[]() isn't legal (foo is array of function returning int *) {precedence of [] and () is higher than that of *} but int (*foo[])() IS legal (foo is array of pointer to function returning int). I know it gets kinda confusing (C can get confusing anyway). Hope this might be a helpful start. (Want a real-life example? Check out the man page for signal(3).) -Alan -- Alan Sparks voice: (508) 480-0881 Internet: sparks@viewlogic.com VIEWlogic Systems Inc., 293 Boston Post Rd. W., Marlboro MA 01752 Disclaimer: VIEWlogic didn't say this; I might have. Who's asking?