Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!ncar!gatech!mcnc!borg!hatteras!morse From: morse@hatteras.cs.unc.edu (Bryan Morse) Newsgroups: comp.lang.c Subject: Re: declarations using pointers to functions Keywords: declaration, pointers, functions Message-ID: <2590@borg.cs.unc.edu> Date: 26 Mar 91 03:55:50 GMT References: <27514@rouge.usl.edu> <1991Mar25.171853.4229@viewlogic.com> Sender: news@cs.unc.edu Organization: University of North Carolina, Chapel Hill Lines: 69 In article <1991Mar25.171853.4229@viewlogic.com> sparks@power.viewlogic.com (Alan Sparks) writes: >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... > [replying explanation of the right-left rule omitted] Actually, I've always found that the easiest was to understand and to write C declarations is to write them out as you would use them. This is one of the simplicities (!?!?) of C declarations. Remember that C declarations are of the form: So, just write the expression. > > buf is array of ten pointers to function returning pointer to int. > > int *(*buf[10])(); > Okay, start with the name: buf Since it is an array, index into it: buf[] Array items are pointers. Pointers are dereferenced, right? *buf[] These pointers are to functions? Functions are called. (Here it is somewhat tricky to remember the precedence, but just remember that right () or [] has higher precedence than left *.) (*buf[])() These functions return pointers. Okay, dereference them. *(*buf[])() These pointers are integers. int *(*buf[])() Fill in the array bounds and add a semicolon and you're done. It's really that simple. Think about how you use it rather than simply applying an English-to-C algorithm. (Disclaimer: Anyone who really writes these things and doesn't use typedefs is crazy.) -- Bryan Morse University of North Carolina at Chapel Hill morse@cs.unc.edu Department of Computer Science