home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 17044 < prev    next >
Encoding:
Text File  |  1992-11-23  |  2.7 KB  |  78 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!stanford.edu!leland.Stanford.EDU!dkeisen
  3. From: dkeisen@leland.Stanford.EDU (Dave Eisen)
  4. Subject: Re: Multiple &'s in an if statement
  5. Message-ID: <1992Nov23.180904.9445@leland.Stanford.EDU>
  6. Keywords: &
  7. Sender: news@leland.Stanford.EDU (Mr News)
  8. Organization: Sequoia Peripherals, Inc.
  9. References: <1992Nov23.164530.19214@iacd>
  10. Distribution: comp.lang.c
  11. Date: Mon, 23 Nov 92 18:09:04 GMT
  12. Lines: 64
  13.  
  14. In article <1992Nov23.164530.19214@iacd> bcochell@ips.iacd.honeywell.com writes:
  15. >Greetings,
  16. >
  17. >Can anyone explain why this works correctly (That is, it checks to see that
  18. >the last 5 characters of name are all digits and that it is null terminated):
  19. >
  20. >   if ( isdigit(name[strlen(path)]) &
  21. >        isdigit(name[(strlen(path) + 1)]) &
  22. >        isdigit(name[(strlen(path) + 2)]) &
  23. >        isdigit(name[(strlen(path) + 3)]) &
  24. >        isdigit(name[(strlen(path) + 4)]) )
  25. >      if ( iscntrl(name[(strlen(path) + 5)]) ) {
  26. >         printf("%s\n",name);
  27. >      }
  28. >
  29. >While the following does not?:
  30. >
  31. >   if ( isdigit(name[strlen(path)]) &
  32. >        isdigit(name[(strlen(path) + 1)]) &
  33. >        isdigit(name[(strlen(path) + 2)]) &
  34. >        isdigit(name[(strlen(path) + 3)]) &
  35. >        isdigit(name[(strlen(path) + 4)]) &
  36. >        (name[(strlen(path) + 5)] == NULL ) ) {
  37. >      printf("%s\n",name);
  38. >   }
  39.  
  40. Because you are using '&' (bitwise and), not '&&' (boolean and) and 
  41. isdigit has some value other than 0 or 1. The functions declared
  42. in <ctype.h>, isdigit, islower, etc. are guaranteed to return some
  43. non-zero value if the character in question staisfies the condition;
  44. they are *not* guaranteed to return 1.
  45.  
  46. So suppose isdigit returns 4 if the character being tested is a 
  47. digit. And suppose that name[strlen (path) + 5] works and returns
  48. 1. (It need not work because NULL can be defined as (void *) 0. Even
  49. if it works it is bad programming practice to use NULL in this
  50. context where it is not a pointer).
  51.  
  52. Then the method that does not work comes down to evaluating
  53.  
  54.    4 & 4 & 4 & 4 & 4 & 1
  55.  
  56. which indeed is equal to 0 because 1 and 4 have no nonzero bits
  57. in common.
  58.  
  59. Change your example to:
  60.  
  61.    if ( isdigit(name[strlen(path)]) &&
  62.         isdigit(name[(strlen(path) + 1)]) &&
  63.         isdigit(name[(strlen(path) + 2)]) &&
  64.         isdigit(name[(strlen(path) + 3)]) &&
  65.         isdigit(name[(strlen(path) + 4)]) &&
  66.         (name[(strlen(path) + 5)] == '\0' ) ) {
  67.       printf("%s\n",name);
  68.    }
  69.  
  70. or even better, something that doesn't recompute strlen (path) 6 times.
  71.  
  72.  
  73. -- 
  74. Dave Eisen                               Sequoia Peripherals: (415) 967-5644
  75. dkeisen@leland.Stanford.EDU              Home:                (415) 321-5154
  76.        There's something in my library to offend everybody. 
  77.           --- Washington Coalition Against Censorship
  78.