declarations using pointers to functions

Bryan Morse morse at hatteras.cs.unc.edu
Tue Mar 26 13:55:50 AEST 1991


In article <1991Mar25.171853.4229 at viewlogic.com> sparks at power.viewlogic.com (Alan Sparks) writes:
>In article <27514 at rouge.usl.edu> anon at 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:

	<type> <expression that evaluates to that type>

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 at cs.unc.edu           Department of Computer Science



More information about the Comp.lang.c mailing list