home *** CD-ROM | disk | FTP | other *** search
/ Danny Amor's Online Library / Danny Amor's Online Library - Volume 1.iso / html / faqs / faq / c-faq / diff < prev    next >
Encoding:
Text File  |  1995-07-25  |  16.5 KB  |  414 lines

  1. Subject: comp.lang.c Changes to Answers to Frequently Asked Questions (FAQ)
  2. Newsgroups: comp.lang.c,comp.answers,news.answers
  3. From: scs@eskimo.com (Steve Summit)
  4. Date: 1 May 94 10:00:53 GMT
  5.  
  6. Archive-name: C-faq/diff
  7. Comp-lang-c-archive-name: C-FAQ-list.diff
  8.  
  9. This article contains a massaged diff list comparing the previous
  10. revision of the comp.lang.c frequently-asked questions list
  11. (last modified March 1, 1994, last posted April 1) and the
  12. current one.  Due to the massaging (which is intended to make the
  13. changes easier to see), this list is not suitable for input to a
  14. patch program.
  15.  
  16. As usual, there are more changes in the works than are reflected
  17. in this month's posting.  If you have made a suggestion or
  18. correction which you think vital, but which you do not see here,
  19. don't panic: though the mills of FAQ list revision grind slowly,
  20. yet they grind exceeding fine (with apologies to von Logau).
  21.  
  22. ==========
  23. < [Last modified March 1, 1994 by scs.]
  24. ---
  25. > [Last modified April 16, 1994 by scs.]
  26. ==========
  27.   1.5:    If NULL were defined as follows:
  28.  
  29. <         #define NULL (char *)0
  30. ---
  31. >         #define NULL ((char *)0)
  32. ==========
  33.   A:    This message, which occurs only under MS-DOS (see, therefore,
  34. <     section 16) means that you've written, via a null pointer, to
  35. <     location zero.
  36. ---
  37. >     section 16) means that you've written, via an unintialized
  38. >     and/or null pointer, to location zero.
  39. ==========
  40. < A:    There is no single perfect method.  Given a function f1()
  41. <     similar to the f() of question 2.10, the array as declared in
  42. <     question 2.10, f2() as declared in question 2.11, array1,
  43. <     array2, array3, and array4 as declared in question 2.14, and a
  44. <     function f3() declared as:
  45. <         f3(pp, m, n)
  46. <         int **pp;
  47. <         int m, n;
  48. <
  49. <     ; the following calls should work as expected:
  50. ---
  51. > A:    There is no single perfect method.  Given the declarations
  52. >         int array[NROWS][NCOLUMNS];
  53. >         int **array1;
  54. >         int **array2;
  55. >         int *array3;
  56. >         int (*array4)[NCOLUMNS];
  57. >     as initialized in the code fragments in questions 2.10 and 2.14,
  58. >     and functions declared as
  59. >         f1(int a[][NCOLUMNS], int m, int n);
  60. >         f2(int *aryp, int nrows, int ncolumns);
  61. >         f3(int **pp, int m, int n);
  62. >
  63. >     (see questions 2.10 and 2.11), the following calls should work
  64. >     as expected:
  65. ==========
  66.   A:    Did the function try to initialize the pointer itself, or just
  67.     what it pointed to?  Remember that arguments in C are passed by
  68.     value.  The called function altered only the passed copy of the
  69. <     pointer.  You'll want to pass the address of the pointer (the
  70. <     function will end up accepting a pointer-to-a-pointer).
  71. ---
  72. >     pointer.  You'll either want to pass the address of the pointer
  73. >     (the function will end up accepting a pointer-to-a-pointer), or
  74. >     have the function return the pointer.
  75. ==========
  76. <     Note that this example also uses fgets instead of gets (always a
  77. <     good idea; see question 11.5), allowing the size of the array to
  78. ---
  79. >     Note that this example also uses fgets() instead of gets()
  80. >     (always a good idea; see question 11.6), allowing the size of
  81. ==========
  82.     The programmer must arrange (explicitly) for sufficient space
  83.     for the results of run-time operations such as string
  84.     concatenation, typically by declaring arrays, or by calling
  85. <     malloc.
  86. ---
  87. >     malloc.  (See also question 17.20.)
  88. ==========
  89.   A:    Make sure that the memory to which the function returns a
  90.     pointer is correctly allocated.  The returned pointer should be
  91.     to a statically-allocated buffer, or to a buffer passed in by
  92. <     the caller, but _not_ to a local (auto) array.  In other words,
  93. <     never do something like
  94. ---
  95. >     the caller, or to memory obtained with malloc(), but _not_ to a
  96. >     local (auto) array.  In other words, never do something like
  97. ==========
  98. <     One fix would to to declare the buffer as
  99. ---
  100. >     One fix (which is imperfect, especially if f() is called
  101. >     recursively, or if several of its return values are needed
  102. >     simultaneously) would to to declare the buffer as
  103.  
  104.             static char buf[10];
  105. ==========
  106.   A:    Before ANSI/ISO Standard C introduced the void * generic pointer
  107.     type, these casts were typically required to silence warnings
  108. <     about assignment between incompatible pointer types.
  109. ---
  110. >     about assignment between incompatible pointer types.  (Under
  111. >     ANSI/ISO Standard C, these casts are not required.)
  112. ==========
  113.   A:    Most implementations of malloc/free do not return freed memory
  114.     to the operating system (if there is one), but merely make it
  115. <     available for future malloc calls.
  116. ---
  117. >     available for future malloc calls within the same process.
  118. ==========
  119.   A:    There is a special exception for those operators, (as well as
  120. <     ?: ); each of them does imply a sequence point (i.e. left-to-
  121. ---
  122. >     the ?: operator); each of them does imply a sequence point (i.e.
  123. ==========
  124.     hand-side.  Use an explicit cast to force long arithmetic:
  125.  
  126.         long int c = (long int)a * b;
  127.  
  128. >     Note that the code (long int)(a * b) would _not_ have the
  129. >     desired effect.
  130. ==========
  131.   5.13:    Is exit(status) truly equivalent to returning status from main?
  132.  
  133. < A:    Essentially, except under a few older, nonconforming systems,
  134. <     and unless data local to main might be needed during cleanup
  135. <     (due perhaps to a setbuf or atexit call).
  136. ---
  137. > A:    Formally, yes, although discrepancies arise under a few older,
  138. >     nonconforming systems, or if data local to main() might be needed
  139. >     during cleanup (due perhaps to a setbuf or atexit call), or if
  140. >     main() is called recursively.
  141. ==========
  142.     Before performing arithmetic, cast the pointer either to char *
  143. <     or to the type you're trying to manipulate.
  144. ---
  145. >     or to the type you're trying to manipulate (but see question
  146. >     2.18).
  147. ==========
  148. < 5.22:    What does #pragma once mean?  I found it in some header files.
  149. ---
  150. > 5.22:    What does "#pragma once" mean?  I found it in some header files.
  151. ==========
  152.   6.9:    How can I list all of the pre#defined identifiers?
  153.  
  154. < A:    There's no standard way, although it is a frequent need.  The
  155. <     most expedient way is probably to extract printable strings from
  156. <     the compiler or preprocessor executable with something like the
  157. <     Unix strings(1) utility.
  158. ---
  159. > A:    There's no standard way, although it is a frequent need.  If the
  160. >     compiler documentation is unhelpful, the most expedient way is
  161. >     probably to extract printable strings from the compiler or
  162. >     preprocessor executable with something like the Unix strings(1)
  163. >     utility.  Beware that many traditional system-selective
  164. >     pre#defined identifiers (e.g. "unix") are non-Standard (because
  165. >     they clash with the user's namespace) and are being removed or
  166. >     renamed.
  167. ==========
  168.     A good rule of thumb is to use TRUE and FALSE (or the like) only
  169. <     for assignment to a Boolean variable, or as the return value
  170. <     from a Boolean function, never in a comparison.
  171. ---
  172. >     for assignment to a Boolean variable or function parameter, or
  173. >     as the return value from a Boolean function, but never in a
  174. >     comparison.
  175. ==========
  176.   10.6:    My compiler is complaining about an invalid redeclaration of a
  177.     function, but I only define it once and call it once.
  178.  
  179. < A:    If the first call precedes the definition, the compiler will
  180. <     assume a function returns an int.  Non-int functions must be
  181. ---
  182. > A:    Functions which are called without a declaration in scope (or
  183. >     before they are declared) are assumed to be declared as
  184. >     returning int, leading to discrepancies if the function is later
  185. >     declared otherwise.  Non-int functions must be declared before
  186. >     they are called.
  187. ==========
  188. < 11.1:    Why doesn't this code:
  189. <
  190. <        char c;
  191. <        while((c = getchar()) != EOF)...
  192. <
  193. <     work?
  194. ---
  195. > 11.1:    What's wrong with this code:
  196. <
  197. <        char c;
  198. <        while((c = getchar()) != EOF)...
  199. ==========
  200.     well as EOF.  By passing getchar's return value through a char,
  201.     either a normal character might be misinterpreted as EOF, or the
  202. <     EOF might be altered and so never seen.
  203. ---
  204. >     EOF might be altered (particularly if type char is unsigned) and
  205. >     so never seen.
  206. ==========
  207. > 11.2:    How can I print a '%' character in a printf format string?  I
  208. >     tried \%, but it didn't work.
  209. > A:    Simply double the percent sign: %% .
  210. >     References: K&R I Sec. 7.3 p. 147; K&R II Sec. 7.2 p. 154; ANSI
  211. >     Sec. 4.9.6.1 .
  212. 2575c2585
  213. < 11.9:    I'm trying to update a file in place, by using fopen mode "r+",
  214. ---
  215. > 11.10:    I'm trying to update a file in place, by using fopen mode "r+",
  216. ==========
  217.   A:    Be sure to call fseek before you write, both to seek back to the
  218.     beginning of the string you're trying to overwrite, and because
  219.     an fseek or fflush is always required between reading and
  220. <     writing in the read/write "+" modes.
  221. ---
  222. >     writing in the read/write "+" modes.  Also, remember that you
  223. >     can only overwrite characters with the same number of
  224. >     replacement characters; see also question 17.4.
  225. ==========
  226.         int mystructcmp(p1, p2)
  227.         char *p1, *p2;        /* const void * for ANSI C */
  228.         {
  229.             struct mystruct *sp1 = (struct mystruct *)p1;
  230.             struct mystruct *sp2 = (struct mystruct *)p2;
  231. <             /* now compare sp1->whatever and *sp2-> ... */
  232. ---
  233. >             /* now compare sp1->whatever and sp2-> ... */
  234. ==========
  235.   12.7:    How can I add n days to a date?  How can I find the difference
  236.     between two dates?
  237.  
  238.   A:    The ANSI/ISO Standard C mktime and difftime functions provide
  239. <     support for both problems.  mktime accepts non-normalized dates,
  240. ---
  241. >     some support for both problems.  mktime() accepts non-normalized
  242. ==========
  243. <     dates to be subtracted.  (Note, however, that these solutions
  244. <     only work for dates which can be represented as time_t's.)  See
  245. <     also questions 12.6 and 17.28.
  246. ---
  247. >     time_t values for two dates to be subtracted.  (Note, however,
  248. >     that these solutions only work for dates in the range which can
  249. >     be represented as time_t's, and that not all days are 86400
  250. >     seconds long.)  See also questions 12.6 and 17.28.
  251. ==========
  252. < 12.12: I'm trying to port this    A:  These routines are variously
  253. <     old program.  Why do I            obsolete; you should
  254. <     get "undefined external"        instead:
  255. ---
  256. > 12.12: I'm trying to port this    A:  Those routines are variously
  257. >     old program.  Why do I            obsolete; you should
  258. >     get "undefined external"        instead:
  259. ==========
  260.     undefined.  Therefore, the order in which libraries are listed
  261.     with respect to object files (and each other) is significant;
  262. <     usually, you want to search the libraries last (i.e., under
  263. <     Unix, put any -l switches towards the end of the command line).
  264. ---
  265. >     usually, you want to search the libraries last.  (For example,
  266. >     under Unix, put any -l switches towards the end of the command
  267. >     line.)
  268. ==========
  269. < 12.16: How can I split up a command line into argc and argv, like the
  270. <     shell does?
  271. ---
  272. > 12.16: How can I split up a command line into whitespace-separated
  273. >     arguments, like main's argc and argv?
  274. ==========
  275. < A:    Most systems have a routine called strtok.
  276. ---
  277. > A:    Most systems have a routine called strtok, although it can be
  278. >     tricky to use and it may not do everything you want it to (e.g.,
  279. >     quoting).
  280. ==========
  281.     The System V release 4 lint is ANSI-compatible, and is available
  282.     separately (bundled with other C tools) from UNIX Support Labs
  283. <     (a subsidiary of AT&T), or from System V resellers.
  284. ---
  285. >     or from System V resellers.
  286. ==========
  287. >     In the absence of lint, many modern compilers attempt to
  288. >     diagnose almost as many problems as a good lint does.
  289. ==========
  290. <         giza.cis.ohio-state.edu    pub/style-guide
  291. ---
  292. >         ftp.cs.umd.edu          pub/style-guide
  293. ==========
  294.   16.7:    How can I check whether a file exists?  I want to query the user
  295. <     if a requested output file already exists.
  296. ---
  297. >     before overwriting existing files.
  298. ==========
  299. < A:    You can try the access() routine, although it's got a few
  300. <     problems.  (It isn't atomic with respect to the following
  301. <     action, and it has anomalies if the program calling it is
  302. <     running as root.)
  303. ---
  304. > A:    On Unix-like systems, you can try the access() routine, although
  305. >     it's got a few problems.  (It isn't atomic with respect to the
  306. >     following action, and can have anomalies if used in setuid
  307. >     programs.)  Another option (perhaps preferable) is to call
  308. >     stat() on the file.  Otherwise, the only guaranteed and portable
  309. >     way to test for file existence is to try opening the file (which
  310. >     doesn't help if you're trying to avoid overwriting an existing
  311. >     file, unless you've got something like the BSD Unix O_EXCL open
  312. >     option available).
  313. ==========
  314.   A:    Unix and some other systems provide a popen() routine, which
  315.     sets up a stdio stream on a pipe connected to the process
  316.     running a command, so that the output can be read (or the input
  317. <     supplied).
  318. ---
  319. >     supplied).  Alternately, invoke the command simply (see question
  320. >     16.12) in such a way that it writes its output to a file, then
  321. >     open and read that file.
  322. ==========
  323. A:    Perhaps you have a pre-ANSI compiler, which doesn't allow
  324.     initialization of "automatic aggregates" (i.e. non-static local
  325.     arrays and structures).  As a workaround, you can make the array
  326. <     global or static.  (You can always initialize local char *
  327. ---
  328. >     global or static, and initialize it with strcpy when f is
  329. >     called.  (You can always initialize local char * variables with
  330. ==========
  331. < 17.4:    How can I delete a line (or record) from the middle of a file?
  332. ---
  333. > 17.4:    How can I insert or delete a line (or record) in the middle of a
  334. >     file?
  335. ==========
  336.     f2c    A Fortran to C converter jointly developed by people
  337.         from Bell Labs, Bellcore, and Carnegie Mellon.  To find
  338. <         about f2c, send the mail message "send index from f2c"
  339. <         to netlib@research.att.com or research!netlib.  (It is
  340. <         also available via anonymous ftp on research.att.com, in
  341. <         directory dist/f2c.)
  342. ---
  343. >         out more about f2c, send the mail message "send index
  344. >         from f2c" to netlib@research.att.com or research!netlib.
  345. >         (It is also available via anonymous ftp on
  346. >         netlib.att.com, in directory netlib/f2c.)
  347. ==========
  348.     (or ask archie; see question 17.12); and MEMDEBUG from
  349. <     dorado.crpht.lu in pub/sources/memdebug .  See also question
  350. <     17.12.
  351. ---
  352. >     ftp.crpht.lu in pub/sources/memdebug .  See also question 17.12.
  353. ==========
  354.   A:    Use mktime (see questions 12.6 and 12.7), or Zeller's
  355. <     congruence.  Here is one quick implementation posted by Tomohiko
  356. <     Sakamoto:
  357. ---
  358. >     congruence, or see the sci.math FAQ list, or try this code
  359. >     posted by Tomohiko Sakamoto:
  360. ==========
  361. <     garbo.uwasa.fi:/pc/c/c-lesson.zip
  362. <     oak.oakland.edu:pub/msdos/c/LEARN-C.ZIP
  363. ---
  364. >     garbo.uwasa.fi:/pc/c-lang/c-lesson.zip
  365. ==========
  366. <     other FAQ lists, including this one: two sites are rtfm.mit.edu
  367. ---
  368. >     other FAQ lists, including this one; two sites are rtfm.mit.edu
  369. ==========
  370.   Acknowledgements
  371.  
  372.   Thanks to Jamshid Afshar, Sudheer Apte, Randall Atkinson, Dan Bernstein,
  373.   Vincent Broman, Stan Brown, Joe Buehler, Gordon Burditt, Burkhard Burow,
  374.   Conor P. Cahill, D'Arcy J.M. Cain, Christopher Calabrese, Ian Cargill,
  375. > Paul Carter, Billy Chambless, Raymond Chen, Jonathan Coxhead, Lee
  376.   Crawford, Steve Dahmer, Andrew Daviel, James Davies, Jutta Degener, Norm
  377.   Diamond, Jeff Dunlop, Ray Dunn, Stephen M. Dunn, Michael J. Eager, Dave
  378.   Eisen, Bjorn Engsig, Chris Flatters, Rod Flores, Alexander Forst, Jeff
  379.   Francis, Dave Gillespie, Samuel Goldstein, Alasdair Grant, Ron
  380.   Guilmette, Doug Gwyn, Tony Hansen, Joe Harrington, Guy Harris, Elliotte
  381.   Rusty Harold, Jos Horsmeier, Blair Houghton, Ke Jin, Kirk Johnson, Larry
  382.   Jones, Kin-ichi Kitano, Peter Klausler, Andrew Koenig, Tom Koenig, Ajoy
  383.   Krishnan T, Markus Kuhn, John Lauro, Felix Lee, Mike Lee, Timothy J.
  384.   Lee, Tony Lee, Don Libes, Christopher Lott, Tim Love, Tim McDaniel,
  385.   Stuart MacMartin, John R. MacMillan, Bob Makowski, Evan Manning, Barry
  386.   Margolin, George Matas, Brad Mears, Bill Mitchell, Mark Moraes, Darren
  387.   Morby, Ken Nakata, Landon Curt Noll, David O'Brien, Richard A. O'Keefe,
  388. > Hans Olsson, Philip (lijnzaad@embl-heidelberg.de), Andrew Phillips,
  389.   Christopher Phillips, Francois Pinard, Dan Pop, Kevin D. Quitt, Pat
  390.   Rankin, J. M. Rosenstock, Erkki Ruohtula, Tomohiko Sakamoto, Rich Salz,
  391.   Chip Salzenberg, Paul Sand, DaviD W. Sanderson, Christopher Sawtell,
  392. > Paul Schlyter, Doug Schmidt, Rene Schmit, Russell Schulz, Patricia
  393.   Shanahan, Peter da Silva, Joshua Simons, Henry Spencer, David Spuler,
  394.   Melanie Summit, Erik Talvola, Clarke Thatcher, Wayne Throop, Chris
  395.   Torek, Andrew Tucker, Goran Uddeborg, Rodrigo Vanegas, Jim Van Zandt,
  396.   Wietse Venema, Ed Vielmetti, Larry Virden, Chris Volpe, Mark Warren,
  397.   Larry Weiss, Freek Wiedijk, Lars Wirzenius, Dave Wolverton, Mitch
  398.   Wright, Conway Yee, and Zhuo Zang, who have contributed, directly or
  399.   indirectly, to this article.  Special thanks to Karl Heuer, and
  400.   particularly to Mark Brader, who (to borrow a line from Steve Johnson)
  401.   have goaded me beyond my inclination, and occasionally beyond my
  402.   endurance, in relentless pursuit of a better FAQ list.
  403. ==========
  404.  
  405.                     Steve Summit
  406.                     scs@eskimo.com
  407.  
  408.