📄 cdecl1.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN"><!-- This collection of hypertext pages is Copyright 1995-2005 by Steve Summit. --><!-- Content from the book "C Programming FAQs: Frequently Asked Questions" --><!-- (Addison-Wesley, 1995, ISBN 0-201-84519-9) is made available here by --><!-- permission of the author and the publisher as a service to the community. --><!-- It is intended to complement the use of the published text --><!-- and is protected by international copyright laws. --><!-- The on-line content may be accessed freely for personal use --><!-- but may not be published or retransmitted without explicit permission. --><!-- --><!-- this page built Sat Dec 24 21:47:45 2005 by faqproc version 2.7 --><!-- from source file decl.sgml dated Wed Dec 21 12:56:18 2005 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/decl/cdecl1.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:44 GMT --><head><meta name=GENERATOR content="faqproc"><title>Question 1.21</title><link href="constparm.html" rev=precedes><link href="recurfuncp.html" rel=precedes><link href="index.html" rev=subdocument></head><body bgcolor="#ffffff"><a href="constparm.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="recurfuncp.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../index-2.html"><img src="../images/buttontop.gif" alt="top/contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><hr><p><!-- qbegin --><h1>comp.lang.c FAQ list<font color=blue>·</font><!-- qtag -->Question 1.21</h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How do Iconstructdeclarations of complicated types such as``array of N pointers to functions returningpointers to functions returning pointersto <TT>char</TT>'',or figure out what similarly complicated declarations mean?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The first part ofthisquestion can be answered inat least three ways:<OL><li><TT>char *(*(*a[N])())();</TT><li>Build the declaration upincrementally,usingtypedefs:<pre> typedef char *pc; /* pointer to char */ typedef pc fpc(); /* function returning pointer to char */ typedef fpc *pfpc; /* pointer to above */ typedef pfpc fpfpc(); /* function returning... */ typedef fpfpc *pfpfpc; /* pointer to... */ pfpfpc a[N]; /* array of... */</pre><li>Use the<TT>cdecl</TT> program,which turns English into C and vice versa.Youprovidea longhand description of the type you want,and <TT>cdecl</TT> responds with the equivalent C declaration:<pre> cdecl> declare a as array of pointer to function returning pointer to function returning pointer to char char *(*(*a[])())()</pre><TT>cdecl</TT> can also explain complicated declarations(yougive ita complicated declarationand it responds with an English description),help with casts,and indicate which set of parentheses the parameters go in(for complicated function definitions, like theoneabove).See question <a href="../resources/tools.html">18.1</a>.</OL></p><p>C's declarationscan be confusingbecausethey come in two parts:a base type,and a <a href="../sx1/index.html#declarator"><dfn>declarator</dfn></a>which contains the identifier or name being declared,perhaps along with <TT>*</TT>'s and <TT>[]</TT>'s and <TT>()</TT>'ssaying whether the name isa pointer to, array of, or function returningthe base type,or some combination.<a href="modifs.html" rel=subdocument>[footnote]</a>For example, in<pre> char *pc;</pre>the base type is <TT>char</TT>,the identifier is <TT>pc</TT>,and the declarator is <TT>*pc</TT>;this tells usthat <TT>*pc</TT> is a <TT>char</TT>(thisis what``declaration mimics use''means).</p><p>One way tomake senseof complicated C declarationsisby reading them ``inside out,''remembering that <TT>[]</TT> and <TT>()</TT>bind more tightly than <TT>*</TT>.For example, given<pre> char *(*pfpc)();</pre>we can see that <TT>pfpc</TT> is a pointer(the inner <TT>*</TT>)to a function(the <TT>()</TT>)to a pointer(the outer <TT>*</TT>)to <TT>char</TT>.When we later use <TT>pfpc</TT>,the expression<TT>*(*pfpc)()</TT>(the value pointed to by the return valueof a function pointed to by <TT>pfpc</TT>)will bea<TT>char</TT>.</p><p>Another way of analyzing these declarations is to decompose the declarator while composing the description, maintaining the ``declaration mimics use'' relationship:<br><br><pre> <TT>*(*pfpc)()</TT> is a <TT>char</TT> <TT>(*pfpc)()</TT> is a pointer to <TT>char</TT> <TT>(*pfpc)</TT> is a function returning pointer to <TT>char</TT> <TT>pfpc</TT> is a pointer to function returning pointer to <TT>char</TT></pre></p><p>If you'd like to make things clearerwhen declaringcomplicatedtypes like these,you can make the analysis explicitby usinga chain of typedefsas in option2 above.</p><p>The pointer-to-function declarations inthe examples abovehave notincludedparameter type information.When the parameters have complicated types,declarations can <em>really</em> getmessy.(Modern versions of <TT>cdecl</TT> can help here, too.)</p><p>Additional links:<br><br>A message of mine explaining the<!-- beware: relative url --><a href="../aryptr/ptrary2.html">difference between array-of-pointer vs. pointer-to-array declarations</a><br><br>David Anderson's``<a href="spiral.anderson.html">Clockwise/Spiral Rule</a>''</p><p>References:K&R2 Sec. 5.12 p. 122<br>ISO Sec. 6.5ff (esp. Sec. 6.5.4)<br>H&S Sec. 4.5 pp. 85-92, Sec. 5.10.1 pp. 149-50<br></p><!-- aend --><p><hr><a href="constparm.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="recurfuncp.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../questions.html"><img src="../images/buttontop.gif" alt="contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><br><!-- lastfooter --><a href="../about.html">about this FAQ list</a> <a href="../eskimo.html">about eskimo</a> <a href="../search.html">search</a> <a href="../feedback.html">feedback</a> <a href="copyright.html">copyright</a><p>Hosted by<a href="http://www.eskimo.com/"><img src="../../www.eskimo.com/img/link/eskitiny.gif" alt="Eskimo North"></a></body><!-- Mirrored from c-faq.com/decl/cdecl1.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:45 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -