📄 faqcatd3c2.html
字号:
(In other words, an undocumented but otherwise global symbolwith a name matching that prefixis, by convention, ``private.'')<li>Use a name beginning with an underscore,since such names shouldn't be used by ordinary code.(See question <a href="faqcatd3c2.html?sec=decl#namespace">1.29</a> for more information,andfora description of the ``no man's land''between the user and implementation namespaces.)</OL></p><p>It may also be possible to use special linkerinvocationsto adjust the visibility of names,but any suchtechniquesare outside of the scope of the C language.<hr><hr><hr><a name="static"><hr><hr><hr><a name="extern"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../decl/extern.html"><!-- qtag -->Question 1.11</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What does <TT>extern</TT> mean in a function declaration?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font><TT>extern</TT> issignificantonlywith data declarations.In function declarations,itcan be used as a stylistic hintto indicate that the function's definitionis probably in another source file,but there is no formaldifferencebetween<pre> extern int f();</pre>and<pre> int f();</pre></p><p>See also question<a href="faqcatd3c2.html?sec=decl#static">1.10</a>.</p><p>References:ISO Sec. 6.1.2.2, Sec. 6.5.1<br>Rationale Sec. 3.1.2.2<br>H&S Secs. 4.3,4.3.1 pp. 75-6<hr><hr><hr><a name="auto"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../decl/auto.html"><!-- qtag -->Question 1.12</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What's the <TT>auto</TT> keyword good for?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Nothing;it's archaic.<a href="../../decl/fn3.html" rel=subdocument>[footnote]</a>(It's a holdover fromC'stypelesspredecessor language B,where in the absence ofkeywords like <TT>int</TT>a declaration always needed a storage class.)See also question<a href="faqcat38c2.html?sec=misc#entry">20.37</a>.</p><p>References:K&R1 Sec. A8.1 p. 193<br>ISO Sec. 6.1.2.4, Sec. 6.5.1<br>H&S Sec. 4.3 p. 75, Sec. 4.3.1 p. 76<hr><hr><hr><a name="typedefvsdefine"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../decl/typedefvsdefine.html"><!-- qtag -->Question 1.13</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What's the difference between using a <TT>typedef</TT>ora <TT>#define</TT>for a user-defined type?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>In general, <TT>typedef</TT>s are preferred,in part because they can correctly encode pointer types.For example,consider these declarations:<pre> typedef char *String_t; #define String_d char * String_t s1, s2; String_d s3, s4;</pre><TT>s1</TT>, <TT>s2</TT>, and <TT>s3</TT> are all declared as <TT>char *</TT>,but<TT>s4</TT> is declared as a <TT>char</TT>,which is probably not the intention.(See also question <a href="faqcatd3c2.html?sec=decl#charstarws">1.5</a>.)</p><p><TT>#define</TT>s do have the advantage that <TT>#ifdef</TT> works on them(see also question <a href="faqcat204f.html?sec=cpp#iftypedef">10.15</a>).On the other hand,<TT>typedef</TT>shave the advantage that they obey scope rules(that is, they can be declared local to a function or block).</p><p>See also questions<a href="faqcatd3c2.html?sec=decl#pfitypedef">1.17</a>,<a href="faqcat6b6b.html?sec=struct#enumvsdefine">2.22</a>,<a href="faqcat7d4b.html?sec=ansi#typedefconst">11.11</a>,and<a href="faqcat744e.html?sec=varargs#funcptr">15.11</a>.</p><p>References:K&R1 Sec. 6.9 p. 141<br>K&R2 Sec. 6.7 pp. 146-7<br>CT&P Sec. 6.4 pp. 83-4<hr><hr><hr><a name="selfrefstruct"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../decl/selfrefstruct.html"><!-- qtag -->Question 1.14</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I can't seem to define a linked listsuccessfully.I tried<pre> typedef struct { char *item; NODEPTR next; } *NODEPTR;</pre>but the compiler gave me error messages.Can't a structure in C contain a pointer to itself?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Structures in C can certainly contain pointers to themselves; thediscussion and example in section 6.5 of K&Rmake this clear.</p><p>The problem withthis example isthe typedef.A typedef defines a new name for a type,and in simpler cases<a href="../../decl/immtypedef.html" rel=subdocument>[footnote]</a>you can define a new structure typeand a typedef for itat the same time,but not in this case.A typedef declaration cannot be used until it is defined,and in the fragment above,it is not yetdefinedat the point wherethe <TT>next</TT> field is declared.</p><p>To fixthis code,firstgive the structure a tag (e.g. ``<TT>struct node</TT>'').Then,declare the <TT>next</TT> fieldasa simple<TT>struct node *</TT>,or disentangle the typedef declaration from the structure definition,or both.Onecorrectedversion would be:<pre> typedef struct node { char *item; struct node *next; } *NODEPTR;</pre>You could also precede the struct declarationwiththe typedef,in which case you could use the <TT>NODEPTR</TT> typedef when declaringthe <TT>next</TT> field,after all:<pre> typedef struct node *NODEPTR; struct node { char *item; NODEPTR next; };</pre>(In this case,you declare a new tyedef name involving <TT>struct node</TT>even though <TT>struct node</TT>has not beencompletelydefinedyet;this you're allowed to do.<a href="../../decl/jst1.html" rel=subdocument>[footnote]</a>)</p><p>Finally,here is a rearrangement incorporating both suggestions:<pre> struct node { char *item; struct node *next; }; typedef struct node *NODEPTR;</pre></p><p>(It's a matter of style which method to prefer;seesection<a href="faqcataae2.html?sec=style#index">17</a>.)</p><p>See also questions<a href="faqcatd3c2.html?sec=decl#mutrefstructs">1.15</a>and<a href="faqcat6b6b.html?sec=struct#typedef">2.1</a>.</p><p>References:K&R1 Sec. 6.5 p. 101<br>K&R2 Sec. 6.5 p. 139<br>ISO Sec. 6.5.2, Sec. 6.5.2.3<br>H&S Sec. 5.6.1 pp. 132-3<hr><hr><hr><a name="mutrefstructs"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../decl/mutrefstructs.html"><!-- qtag -->Question 1.15</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I define a pair of mutually referential structures?I tried<pre> typedef struct { int afield; BPTR bpointer; } *APTR; typedef struct { int bfield; APTR apointer; } *BPTR;</pre>but the compiler doesn't know about<TT>BPTR</TT>when it is used inthe first structure declaration.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>As in question <a href="faqcatd3c2.html?sec=decl#selfrefstruct">1.14</a>,the problem lies not inthe structures orthe pointers but the typedefs.First,give the two structures tags,anddefinethe link pointerswithout using typedefs:<pre> struct a { int afield; struct b *bpointer; }; struct b { int bfield; struct a *apointer; };</pre>The compiler can accept the field declaration <TT>struct b *bpointer</TT>within <TT>struct a</TT>,even though it has not yet heard of<TT>struct b</TT>.(<TT>struct b</TT> is``incomplete''at that point.)Occasionally it is necessary to precede this couplet with theempty declaration<pre> struct b;</pre>to mask the declarations (if in an inner scope) from a different<TT>struct b</TT> in an outer scope.</p><p>After declaring the two structures using struct tags,you can then declare the typedefs separately:<pre> typedef struct a *APTR; typedef struct b *BPTR;</pre></p><p>Alternatively,you can define the typedefsbefore the struct definitions<a href="../../decl/jst2.html" rel=subdocument>[footnote]</a>,in which case you can use them when declaring the link pointer fields:<pre> typedef struct a *APTR; typedef struct b *BPTR; struct a { int afield; BPTR bpointer; }; struct b { int bfield; APTR apointer; };</pre></p><p>See also question <a href="faqcatd3c2.html?sec=decl#selfrefstruct">1.14</a>.</p><p>References:K&R2 Sec. 6.5 p. 140<br>ISO Sec. 6.5.2.3<br>H&S Sec. 5.6.1 p. 132<hr><hr><hr><a name="structtypdf"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../decl/structtypdf.html"><!-- qtag -->Question 1.16</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What's the difference betweenthese two declarations?<pre> struct x1 { ... }; typedef struct { ... } x2;</pre></p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat6b6b.html?sec=struct#typedef">2.1</a>.<hr><hr><hr><a name="pfitypedef"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../decl/pfitypedef.html"><!-- qtag -->Question 1.17</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What does<pre>typedef int (*funcptr)();</pre>mean?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It defines a typedef, <TT>funcptr</TT>, for a pointer to afunction(taking unspecified arguments)returning an <TT>int</TT>.It can be used todeclareone or morepointers to functions:<pre> funcptr pf1, pf2;</pre>which is equivalent to the more verbose,and perhaps harder to understand<pre> int (*pf1)(), (*pf2)();</pre>See also questions<a href="faqcatd3c2.html?sec=decl#cdecl1">1.21</a>,<a href="faqcatabdc.html?sec=ptrs#funccall">4.12</a>,and<a href="faqcat744e.html?sec=varargs#funcptr">15.11</a>.</p><p>References:K&R1 Sec. 6.9 p. 141<br>K&R2 Sec. 6.7 p. 147<hr><hr><hr><a name="typdfcnst"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../decl/typdfcnst.html"><!-- qtag -->Question 1.18</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I've gotthe declarations<pre> typedef char *charp; const charp p;</pre>Why is <TT>p</TT> turning out <TT>const</TT>,instead of the characters pointed to?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat7d4b.html?sec=ansi#typedefconst">11.11</a>.<hr><hr><hr><a name="constasconst"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../decl/constasconst.html"><!-- qtag -->Question 1.19</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I don't understand why I can'tuse <TT>const</TT> values in initializers and arraydimensions,as in<pre> const int n = 5; int a[n];</pre></p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat7d4b.html?sec=ansi#constasconst">11.8</a>.<hr><hr><hr><a name="constptrconst"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../decl/constptrconst.html"><!-- qtag -->Question 1.20</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What's the difference between<TT>const char *p</TT>,<TT>char const *p</TT>,and <TT>char * const p</TT>?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See questions<a href="faqcat7d4b.html?sec=ansi#constptrconst">11.9</a>and<a href="faqcatd3c2.html?sec=decl#cdecl1">1.21</a>.<hr><hr><hr><a name="constparm"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../decl/constparm.html"><!-- qtag -->Question 1.20b</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What does it mean for a function parameter to be <TT>const</TT>?What do the two <TT>const</TT>'s in<pre> int f(const * const p)</pre>mean?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>In<pre> int f(const * const p)</pre>the first of the two <TT>const</TT>'sis perfectly appropriate and quite useful; manyfunctions declare parameters which are pointers to const data,and doing so documents (and tends to enforce) the function'spromise that it won't modify the pointed-to data in the caller.The second <TT>const</TT>, on the other hand, is almost useless; all itsays is that the function won't alter its own copy of thepointer, even though it wouldn't cause the caller <em>or</em> thefunction any problems if it did, nor is this anything the callershould care about in any case.The situation is the same as if afunction declared an ordinary (non-pointer) parameter as const:<pre> int f2(const int x)</pre>This says that nowhere in the body of <TT>f2()</TT> will the functionassign a different value to <TT>x</TT>.(Compilers should try to enforce this promise, too.)But assigning adifferent value to <TT>x</TT> wouldn't affect the value that the callerhad passed (because C always uses call-by-value), so it's anunimportant guarantee, and in fact a pretty useless one, becausewhat does the function gain by promising (to itself, since it'sthe only one that could care) whether it will or won't bemodifying in the passed-in copy of the value?<hr><hr><hr><a name="cdecl1"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../decl/cdecl1.html"><!-- qtag -->Question 1.21</a></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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -