📄 questions.html
字号:
how can<pre> int f(char str[]) { if(str[0] == '\0') str = "none"; ... }</pre>work?</p><p><a href="aryptr/aryasgn3.html" rel=subdocument>6.6b</a>And what about this?Isn't this an array assignment?<pre> char a[] = "Hello, world!\n";</pre></p><p><a href="aryptr/arraylval.html" rel=subdocument>6.7</a>How can an array be an lvalue, if you can't assign to it?</p><p><a href="aryptr/practdiff.html" rel=subdocument>6.8</a>Practically speaking,whatis thedifference between arrays and pointers?</p><p><a href="aryptr/constptr.html" rel=subdocument>6.9</a>Someone explained to me that arrays were really just constant pointers.</p><p><a href="aryptr/ptrkindofary.html" rel=subdocument>6.10</a>I'm still mystified.Is a pointer a kind of array,or is an array a kind of pointer?</p><p><a href="aryptr/joke.html" rel=subdocument>6.11</a>I came across some ``joke'' code containing the ``expression''<TT>5["abcdef"]</TT> .How can this be legal C?</p><p><a href="aryptr/aryvsadr.html" rel=subdocument>6.12</a>Since array references decay into pointers,if <TT>arr</TT> is an array,what'sthe difference between<TT>arr</TT> and <TT>&arr</TT>?</p><p><a href="aryptr/ptrtoarray.html" rel=subdocument>6.13</a>How do I declare a pointer to an array?</p><p><a href="aryptr/dynarray.html" rel=subdocument>6.14</a>How can I set an array's size at run time?<br>How can I avoidfixed-sized arrays?</p><p><a href="aryptr/dynlocarys.html" rel=subdocument>6.15</a>How can I declare local arrays of a size matching a passed-in array?</p><p><a href="aryptr/dynmuldimary.html" rel=subdocument>6.16</a>How can I dynamically allocate a multidimensional array?</p><p><a href="aryptr/non0based.html" rel=subdocument>6.17</a>Here's a neat trick: if I write<pre> int realarray[10]; int *array = &realarray[-1];</pre>I can treat <TT>array</TT> as if it were a 1-based array.</p><p><a href="aryptr/pass2dary.html" rel=subdocument>6.18</a>My compiler complained when I passed a two-dimensional array to afunction expecting a pointer to a pointer.</p><p><a href="aryptr/ary2dfunc2.html" rel=subdocument>6.19</a>How do I write functions which accept two-dimensional arrayswhen the width is not known at compile time?</p><p><a href="aryptr/ary2dfunc3.html" rel=subdocument>6.20</a>How can I use statically- and dynamically-allocatedmultidimensional arrays interchangeably when passing them to functions?</p><p><a href="aryptr/aryparmsize.html" rel=subdocument>6.21</a>Why doesn't <TT>sizeof</TT> properly report the size of an arraywhen the arrayis a parameter to a function?I have a test routine<pre> f(char a[10]) { int i = sizeof(a); printf("%d\n", i); }</pre>and it prints4, not 10.</p><p><a href="aryptr/extarysize.html" rel=subdocument>6.22</a>How can code in a file where an array is declared as <TT>extern</TT>(i.e. it is defined, and its size determined, in some other file)determine the size of the array?<TT>sizeof</TT> doesn't seem to work.</p><p><a href="aryptr/arraynels.html" rel=subdocument>6.23</a>I want to know how many elements are in an array,but <TT>sizeof</TT> yields the size in bytes.</p><p><a href="aryptr/arybits.html" rel=subdocument>6.24</a>Is there a way to have an array of bits?</p><hr><H4>7. Memory Allocation</H4><p><a href="malloc/malloc1.html" rel=subdocument>7.1</a>Why doesn'tthis fragmentwork?<pre> char *answer; printf("Type something:\n"); gets(answer); printf("You typed \"%s\"\n", answer);</pre></p><p><a href="malloc/malloc2.html" rel=subdocument>7.2</a>I can't get <TT>strcat</TT> to work.I tried<pre> char *s1 = "Hello, "; char *s2 = "world!"; char *s3 = strcat(s1, s2);</pre>but I got strange results.</p><p><a href="malloc/attitude.html" rel=subdocument>7.3</a>But theman pagefor <TT>strcat</TT> says thatit takes two <TT>char *</TT>'s as arguments.How amI supposed to know to allocate things?</p><p><a href="malloc/lucky.html" rel=subdocument>7.3b</a>I just tried the code<pre>char *p;strcpy(p, "abc");</pre>and it worked.How?Why didn't it crash?</p><p><a href="malloc/ptrvaralloc.html" rel=subdocument>7.3c</a>How much memory does a pointer variable allocate?</p><p><a href="malloc/linebfdur.html" rel=subdocument>7.4</a>I'mreadinglines from a file into anarray,with this code:<pre> char linebuf[80]; char *lines[100]; int i; for(i = 0; i < 100; i++) { char *p = fgets(linebuf, 80, fp); if(p == NULL) break; lines[i] = p; }</pre>Why do all the lines end up containing copies of the last line?</p><p><a href="malloc/retaggr.html" rel=subdocument>7.5a</a>I have a function that is supposed to return a string,but when it returns to its caller,the returned string is garbage.</p><p><a href="malloc/retaggr2.html" rel=subdocument>7.5b</a>So what's the right way to return a stringor other aggregate?</p><p><a href="malloc/decl.html" rel=subdocument>7.6</a>Why am I getting``warning: assignment of pointer from integer lacks a cast''for calls to <TT>malloc</TT>?</p><p><a href="malloc/cast.html" rel=subdocument>7.7</a>Why does some code carefully cast the values returned by <TT>malloc</TT>to the pointer type being allocated?</p><p><a href="malloc/mallocnocast.html" rel=subdocument>7.7b</a>What's wrong with casting <TT>malloc</TT>'s return value?</p><p><a href="malloc/mallocnocast2.html" rel=subdocument>7.7c</a>In a call to <TT>malloc</TT>,what does an error like``Cannot convert `<TT>void *</TT>' to `<TT>int *'''mean?</TT></p><p><a href="malloc/sizeofchar.html" rel=subdocument>7.8</a>I see code like<pre> char *p = malloc(strlen(s) + 1); strcpy(p, s);</pre>Shouldn't that be <TT>malloc((strlen(s) + 1) * sizeof(char))</TT>?</p><p><a href="malloc/mymallocretp.html" rel=subdocument>7.9</a>I wrote a little wrapper around <TT>malloc</TT>,but it doesn't work:<pre> #include <stdio.h> #include <stdlib.h> mymalloc(void *retp, size_t size) { retp = malloc(size); if(retp == NULL) { fprintf(stderr, "out of memory\n"); exit(EXIT_FAILURE); } }</pre></p><p><a href="malloc/mimic3.html" rel=subdocument>7.10</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="malloc/fcninit2.html" rel=subdocument>7.10a</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="malloc/lintpalign2.html" rel=subdocument>7.10b</a>How can I shut off the``warning: possible pointer alignment problem''messagewhich<TT>lint</TT> gives me for each call to <TT>malloc</TT>?</p><p><a href="malloc/dynarray2.html" rel=subdocument>7.11</a>How can I dynamically allocate arrays?</p><p><a href="malloc/memavail2.html" rel=subdocument>7.12</a>How can I find out how much memory is available?</p><p><a href="malloc/malloc0xref.html" rel=subdocument>7.13</a>What should <TT>malloc(0)</TT> do?Return a null pointer or a pointer to 0 bytes?</p><p><a href="malloc/lazyalloc.html" rel=subdocument>7.14</a>I've heard that some operating systemsdon't actually allocate <TT>malloc</TT>'ed memoryuntiltheprogram tries touseit.Is this legal?</p><p><a href="malloc/sizetlong.html" rel=subdocument>7.15</a><TT>malloc</TT>isreturning crazy pointer values,but I <em>did</em> read question <a href="decl.html">7.6</a>and I have included the line<pre> extern void *malloc();</pre>before I call it.</p><p><a href="malloc/sizewrap.html" rel=subdocument>7.16</a>I'm allocating a large array for some numeric work,using the line<pre> double *array = malloc(300 * 300 * sizeof(double));</pre><TT>malloc</TT> isn't returning null,butthe program is acting strangely,as if it's overwriting memory,or <TT>malloc</TT> isn't allocating as much as I asked for,or something.</p><p><a href="malloc/segment.html" rel=subdocument>7.17</a>I've got 8 meg of memory in my PC.Why can I only seem to <TT>malloc</TT> 640K or so?</p><p><a href="malloc/efficiency.html" rel=subdocument>7.18</a>My application depends heavily ondynamic allocation of nodes for data structures,and <TT>malloc</TT>/<TT>free</TT> overheadisbecominga bottleneck.What canI do?</p><p><a href="malloc/crash.html" rel=subdocument>7.19</a>My program is crashing, apparently somewhere down inside<TT>malloc</TT>,but I can't see anything wrong with it.Is there a bug in <TT>malloc</TT>?</p><p><a href="malloc/noscale.html" rel=subdocument>7.19b</a></p><p>I'm dynamically allocating an array, like this:<pre> int *iarray = (int *)malloc(nints);</pre><TT>malloc</TT> isn't returning NULL,but the code isn't working.</p><p><a href="malloc/useafterfree.html" rel=subdocument>7.20</a>You can't use dynamically-allocated memory after you free it, can you?</p><p><a href="malloc/ptrafterfree.html" rel=subdocument>7.21</a>Why isn't a pointernullafter calling<TT>free</TT>?<br>How unsafe is it to use(assign, compare)a pointer value after it's been freed?</p><p><a href="malloc/local.html" rel=subdocument>7.22</a>When I call <TT>malloc</TT> to allocate memory for apointer which is local to a function,do I have to explicitly <TT>free</TT> it?</p><p><a href="malloc/freeforall.html" rel=subdocument>7.23</a>I'm allocating structures which contain pointers to otherdynamically-allocated objects.When I freea structure,do Ialsohave to free each subsidiary pointer?</p><p><a href="malloc/freeb4exit.html" rel=subdocument>7.24</a>Must I free allocated memory before the program exits?</p><p><a href="malloc/freetoOS.html" rel=subdocument>7.25</a>I have a program which <TT>malloc</TT>sand later<TT>free</TT>s a lotof memory,butI can see from the operating systemthatmemory usagedoesn'tactuallygo back down.</p><p><a href="malloc/freesize.html" rel=subdocument>7.26</a>How does <TT>free</TT> know how many bytes to free?</p><p><a href="malloc/querysize.html" rel=subdocument>7.27</a>So can I query the malloc package to find out how big anallocated block is?</p><p><a href="malloc/sizeof.html" rel=subdocument>7.28</a>Why doesn't <TT>sizeof</TT> tell me the size ofthe block of memory pointed to by a pointer?</p><p><a href="malloc/realloc.html" rel=subdocument>7.29</a>Having dynamically allocated an array(as in question <a href="aryptr/dynarray.html">6.14</a>),can I change its size?</p><p><a href="malloc/reallocnull.html" rel=subdocument>7.30</a>Is it legal to pass a null pointer as the first argument to <TT>realloc</TT>?Why would you want to?</p><p><a href="malloc/calloc.html" rel=subdocument>7.31</a>What's the difference between <TT>calloc</TT> and <TT>malloc</TT>?Which should Iuse?Is it safe to take advantage of <TT>calloc</TT>'szero-filling?Does <TT>free</TT> workon memory allocated with <TT>calloc</TT>,or do youneed a <TT>cfree</TT>?</p><p><a href="malloc/alloca.html" rel=subdocument>7.32</a>What is <TT>alloca</TT> and why is its use discouraged?</p><hr><H4>8. Characters and Strings</H4><p><a href="charstring/strvschar.html" rel=subdocument>8.1</a>Why doesn't<pre>strcat(string, '!');</pre>work?</p><p><a href="charstring/stringeq.html" rel=subdocument>8.2</a>I'm checking a string to see if it matches a particular value.Why isn't this code working?<pre> char *string; ... if(string == "value") { /* string matches "value" */ ... }</pre></p><p><a href="charstring/assign.html" rel=subdocument>8.3</a>If I can say<pre> char a[] = "Hello, world!";</pre>why can't I say<pre> char a[14]; a = "Hello, world!";</pre></p><p><a href="charstring/malloc.html" rel=subdocument>8.4</a>I can't get <TT>strcat</TT> to work.I tried<pre> char *s1 = "Hello, "; char *s2 = "world!"; char *s3 = strcat(s1, s2);</pre>but I got strange results.</p><p><a href="charstring/strlitinit.html" rel=subdocument>8.5</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="charstring/asciivals.html" rel=subdocument>8.6</a>How can I get thenumericvalue(i.e. ASCII or other character set code)corresponding to acharacter,or vice versa?</p><p><a href="charstring/substr.html" rel=subdocument>8.7</a>Does C have anything like the ``substr''(extract substring)routine present in other languages?</p><p><a href="charstring/runtimesc.html" rel=subdocument>8.8</a>I'm readingstringstyped bythe userinto an array,and then printing them out later.When the user typesa sequence like <TT>\n</TT>,whyisn't it beinghandled properly?</p><p><a href="charstring/sizeofchar.html" rel=subdocument>8.9</a>I think something's wrong with my compiler:I just noticed that <TT>sizeof('a')</TT> is 2, not 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -