⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 questions.html

📁 this is a mirrored site c-faq. thought might need offline
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!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 all.sgml dated Wed Dec 21 15:28:12 2005 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/questions.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:57:21 GMT --><head><meta name=GENERATOR content="faqproc"><title>All Questions</title></head><body bgcolor="#ffffff">&nbsp;<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><hr><H4>1. Declarations and Initializations</H4><p><a href="decl/inttypes.html" rel=subdocument>1.1</a>How should I decide which integer type to use?</p><p><a href="decl/exactsizes.html" rel=subdocument>1.2</a>Why aren't the sizes of the standard types precisely defined?</p><p><a href="decl/int16.html" rel=subdocument>1.3</a>Since C doesn't define sizes exactly,I've been usingtypedefslike <TT>int16</TT> and<TT>int32</TT>.I can then define these typedefsto be <TT>int</TT>, <TT>short</TT>, <TT>long</TT>,etc. depending on what machine I'm using.That should solve everything, right?</p><p><a href="decl/octabyte.html" rel=subdocument>1.4</a>What should the 64-bit type beon amachine that can support it?</p><p><a href="decl/charstarws.html" rel=subdocument>1.5</a>What's wrong withthisdeclaration?<pre>char* p1, p2;</pre>I get errors when I try touse<TT>p2</TT>.</p><p><a href="decl/mimic.html" rel=subdocument>1.6</a>I'm trying to declare a pointer and allocate some space for it,but it'snot working.What's wrong withthis code?<pre>char *p;*p = malloc(10);</pre></p><p><a href="decl/decldef.html" rel=subdocument>1.7</a>What's the best way to declare and defineglobal variablesand functions?</p><p><a href="decl/opaque.html" rel=subdocument>1.8</a>How can I implementopaque (abstract) data types in C?</p><p><a href="decl/semiglobal.html" rel=subdocument>1.9</a>How can I make a sort of ``semi-global'' variable,that is,one that'sprivate to a few functions spread across a few source files?</p><p><a href="decl/static.html" rel=subdocument>1.10</a>Doalldeclarations forthe same<TT>static</TT> functionor variablehave to include the storage class <TT>static</TT>?</p><p><a href="decl/extern.html" rel=subdocument>1.11</a>What does <TT>extern</TT> mean in a function declaration?</p><p><a href="decl/auto.html" rel=subdocument>1.12</a>What's the <TT>auto</TT> keyword good for?</p><p><a href="decl/typedefvsdefine.html" rel=subdocument>1.13</a>What's the difference between using a <TT>typedef</TT>ora <TT>#define</TT>for a user-defined type?</p><p><a href="decl/selfrefstruct.html" rel=subdocument>1.14</a>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><a href="decl/mutrefstructs.html" rel=subdocument>1.15</a>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><a href="decl/structtypdf.html" rel=subdocument>1.16</a>What's the difference betweenthese two declarations?<pre>	struct x1 { ... };	typedef struct { ... } x2;</pre></p><p><a href="decl/pfitypedef.html" rel=subdocument>1.17</a>What does<pre>typedef int (*funcptr)();</pre>mean?</p><p><a href="decl/typdfcnst.html" rel=subdocument>1.18</a>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><a href="decl/constasconst.html" rel=subdocument>1.19</a>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><a href="decl/constptrconst.html" rel=subdocument>1.20</a>What's the difference between<TT>const&nbsp;char&nbsp;*p</TT>,<TT>char&nbsp;const&nbsp;*p</TT>,and <TT>char&nbsp;*&nbsp;const&nbsp;p</TT>?</p><p><a href="decl/constparm.html" rel=subdocument>1.20b</a></p><p>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><a href="decl/cdecl1.html" rel=subdocument>1.21</a>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><a href="decl/recurfuncp.html" rel=subdocument>1.22</a>How can I declare a functionthat can returna pointer to a functionof the same type?I'm building a state machine with one functionfor each state, each of which returns a pointer to the function forthe next state.But I can't find a way to declare thefunctions--I seemto need a functionreturning a pointer to a functionreturning a pointer to a functionreturning a pointer to a function...,ad infinitum.</p><p><a href="decl/dynarray.html" rel=subdocument>1.23</a>Can I declare a local array(or parameter array)of a size matching a passed-in array,or set by another parameter?</p><p><a href="decl/extarraysize.html" rel=subdocument>1.24</a>I have an <TT>extern</TT> arraywhich is definedinone file,and used in another:<pre><I>file1.c:</I>			<I>file2.c:</I>int array[] = {1, 2, 3};	extern int array[];</pre>Why doesn't <TT>sizeof</TT> work on<TT>array</TT> in <TT>file2.c</TT>?</p><p><a href="decl/implfdecl.html" rel=subdocument>1.25</a>My compiler is complainingabout an invalid redeclaration of a function,but I only define it onceand call it once.</p><p><a href="decl/main.html" rel=subdocument>1.25b</a>What's the right declaration for <TT>main</TT>?<br>Is <TT>void&nbsp;main()</TT> correct?</p><p><a href="decl/argpromo.html" rel=subdocument>1.26</a>My compiler is complaining about mismatched function prototypeswhich look fine to me.</p><p><a href="decl/headerglom.html" rel=subdocument>1.27</a>I'm getting strangesyntax errorson the very firstdeclaration ina file,but it looks fine.</p><p><a href="decl/bigdatastr.html" rel=subdocument>1.28</a>My compiler isn't letting me declare a big arraylike<pre>double array[256][256];</pre></p><p><a href="decl/namespace.html" rel=subdocument>1.29</a>How can I determinewhich identifiers are safe for me to useand which are reserved?</p><p><a href="decl/initval.html" rel=subdocument>1.30</a>Whatam I allowed toassume about the initial values of variablesand arrayswhich are not explicitly initialized?<br>If global variables start out as ``zero'',is that good enoughfor null pointers and floating-point zeroes?</p><p><a href="decl/autoaggrinit.html" rel=subdocument>1.31</a>This code, straight out of a book, isn't compiling:<pre>int f(){	char a[] = "Hello, world!";}</pre></p><p><a href="decl/fcninit.html" rel=subdocument>1.31b</a>What's wrong withthis initialization?<pre>char *p = malloc(10);</pre>My compiler is complaining aboutan ``invalid initializer'',or something.</p><p><a href="decl/strlitinit.html" rel=subdocument>1.32</a>What is the difference betweenthese initializations?<pre>char a[] = "string literal";char *p  = "string literal";</pre>My programcrashes ifI try toassign a new value to <TT>p[i]</TT>.</p><p><a href="decl/nonstring.html" rel=subdocument>1.33</a>Is <TT>char&nbsp;a[3]&nbsp;=&nbsp;"abc";</TT> legal?</p><p><a href="decl/ptrfuncinit.html" rel=subdocument>1.34</a>I finally figured out the syntax for declaring pointers tofunctions, but now how do I initialize one?</p><p><a href="decl/initunion.html" rel=subdocument>1.35</a>Can I initialize unions?</p><hr><H4>2. Structures, Unions, and Enumerations</H4><p><a href="struct/typedef.html" rel=subdocument>2.1</a>What's the difference betweenthese two declarations?<pre>	struct x1 { ... };	typedef struct { ... } x2;</pre></p><p><a href="struct/impltypedef.html" rel=subdocument>2.2</a>Why doesn't<pre>struct x { ... };x thestruct;</pre>work?</p><p><a href="struct/selfref.html" rel=subdocument>2.3</a>Can a structure contain a pointerto itself?</p><p><a href="struct/opaquetypes.html" rel=subdocument>2.4</a>How can I implementopaque (abstract) data types in C?</p><p><a href="struct/oop.html" rel=subdocument>2.4b</a>Is there a good way of simulating OOP-style inheritance,or other OOP features,in C?</p><p><a href="struct/fcnproto.html" rel=subdocument>2.5</a>Why does the declaration<pre>extern int f(struct x *p);</pre>give mean obscurewarning messageabout``struct x declared inside parameter list''?</p><p><a href="struct/structhack.html" rel=subdocument>2.6</a>I came across some code that declared a structurelike this:<pre>struct name {	int namelen;	char namestr[1];};</pre>and then did some tricky allocation to makethe<TT>namestr</TT>array act like ithad several elements,with the numberrecorded by <TT>namelen</TT>.How does this work?Isitlegal or portable?</p><p><a href="struct/firstclass.html" rel=subdocument>2.7</a>I heardthat structures could be assignedto variables and passed to and from functions, but K&amp;R1saysnot.</p><p><a href="struct/compare.html" rel=subdocument>2.8</a>Is there a way tocompare structuresautomatically?</p><p><a href="struct/passret.html" rel=subdocument>2.9</a>Howarestructure passing and returningimplemented?</p><p><a href="struct/anonstruct.html" rel=subdocument>2.10</a>How canI pass constant values to functions which accept structure arguments?How can I create nameless, immediate, constant structure values?</p><p><a href="struct/io.html" rel=subdocument>2.11</a>How can I read/write structures from/todata files?</p><p><a href="struct/padding.html" rel=subdocument>2.12</a>Why is mycompilerleaving holes in structures,wasting spaceand preventing``binary'' I/O to external datafiles?CanI turnthisoff,or otherwise control the alignment of structure fields?</p><p><a href="struct/endpad.html" rel=subdocument>2.13</a>Why does <TT>sizeof</TT> report a larger size than I expect for astructure type,as if there were padding at the end?</p><p><a href="struct/offsetof.html" rel=subdocument>2.14</a>How can I determine the byte offset of a field within a structure?</p><p><a href="struct/fieldnames.html" rel=subdocument>2.15</a>How can I access structure fields by name at run time?</p><p><a href="struct/withxref.html" rel=subdocument>2.16</a>Does C have an equivalent to Pascal's<TT>with</TT> statement?</p><p><a href="struct/decay.html" rel=subdocument>2.17</a>If an array name acts like a pointer to the base of an array,why isn't the same thing true of a structure?</p><p><a href="struct/retcrash.html" rel=subdocument>2.18</a>This programworks correctly, butitdumps coreafter itfinishes.Why?<pre>	struct list {		char *item;		struct list *next;	}	/* Here is the main program. */	main(argc, argv)	{ ... }</pre></p><p><a href="struct/union.html" rel=subdocument>2.19</a>What's the difference between a structure and a union, anyway?

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -