home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / c / 12905 < prev    next >
Encoding:
Internet Message Format  |  1992-08-29  |  1.3 KB

  1. From: Monroe.Thomas@ttlg.UUCP (Monroe Thomas)
  2. Sender: postmaster@ttlg.UUCP
  3. Path: sparky!uunet!sun-barr!decwrl!access.usask.ca!kakwa.ucs.ualberta.ca!alberta!ttlg!postmaster
  4. Newsgroups: comp.lang.c
  5. Subject: Strange error in C compi
  6. Message-ID: <714996010.0@ttlg.ttlg.UUCP>
  7. Date: 28 Aug 92  02:12:00 mst
  8. Lines: 46
  9.  
  10. > Hello all!
  11.  
  12. >Consider the following code:
  13.  
  14. >/*
  15. > _DYNAMIC is linker-defined and may be either 0 or non-zero,
  16. > depending on link mode.
  17. >*/
  18.  
  19. >extern _DYNAMIC[];
  20.  
  21. >main() {
  22. > if (_DYNAMIC)
  23. >  puts("Dynamic");
  24. > else
  25. >  puts("Static");
  26. >}
  27.  
  28. > The above code doesn't work, but with if-statement rewritten
  29. > as follows does:
  30.  
  31. > if (_DYNAMIC != (int*) 0)
  32.  
  33. > Could anyone explain why?
  34.  
  35. > Note: in the first case compiler treats else-clause as dead
  36. > code, but in the second one everything is compiled as
  37. > expected.
  38.  
  39. Obviously your compiler as treating _DYNAMIC as never being a null
  40. pointer.  Your first construct is testing the address of the variable,
  41. your second example is testing the value at that address.
  42.  
  43. Try dereferencing _DYNAMIC to get a construct similar to your first:
  44.  
  45. main() {
  46.  if (*_DYNAMIC)
  47.    // note use of *
  48.    puts("Dynamic");
  49.  else
  50.    puts("Static");
  51. }
  52.  
  53.  * OLX 2.2 * "The little demon was deceitful," Tom implied.
  54.  
  55.  * Origin: Through the Looking Glass (42:100/14)
  56.