home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18243 < prev    next >
Encoding:
Text File  |  1992-12-12  |  2.9 KB  |  84 lines

  1. Path: sparky!uunet!nwnexus!beauty!rwing!pat
  2. From: pat@rwing.UUCP (Pat Myrto)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: ...so I typed this in straight from the book...
  5. Message-ID: <1836@rwing.UUCP>
  6. Date: 12 Dec 92 05:57:59 GMT
  7. References: <1g108jINNdg8@terminator.rs.itd.umich.edu> <1992Dec8.153936.15082@prl.philips.nl>
  8. Organization: Totally Unorganized
  9. Lines: 73
  10.  
  11. In article <1992Dec8.153936.15082@prl.philips.nl> ribeiro@prl.philips.nl (ribeiro ma) writes:
  12. :In article <1g108jINNdg8@terminator.rs.itd.umich.edu> colinj@monet.ccs.itd.umich.edu (Colin Eric Johnson) writes:
  13. :>I typed this in straight in from the Kelly & Pohl book and it
  14. :>dumps core every time I try to run it. Lint Hates it (says
  15. :>main returns a random value).
  16. :>
  17. :>What's up with that?
  18. :>---------------------
  19. :>
  20. :>#include   <stdio.h>
  21. :>#include   <string.h>
  22. :>
  23. :>#define    MAXSTRING   100
  24. :>
  25. :>main()
  26. :>{
  27. :>     char  c = 'a', *p, s[MAXSTRING];
  28. :>
  29. :>     p = &c;
  30. :>     printf("%c%c%c ", *p, *p + 1, *p + 2);
  31. :>     strcpy(s, "ABC");
  32. :>     printf("%s %c%c%s\n", s, *s + 6, *s + 7, *s + 1);
  33. :>     strcpy(s, "she sells sea shell by the seashore");
  34. :>     p = s + 14;
  35. :>     for ( ; *p != '\0'; ++p) {
  36. :>      if (*p == 'e')
  37. :>           *p = 'E';
  38. :>      if (*p == ' ')
  39. :>           *p = '\n';
  40. :>     }
  41. :>     printf("%s\n", s);
  42. :>}
  43. :    This piece of code compiles and runs fine with the standard cc equaliser
  44. :available with version 8.05 of HPUX. The output it gives is:
  45.  
  46. I tried it and it ran for me, too, but that doesn't mean the code is right.
  47.  
  48. I think that is just luck of how memory is laid out that it ran at all.
  49. The last expression in the printf() stmt under question (that is, 
  50. '*s + 1') evaluates to a CHARACTER.  The format specifier is for a STRING,
  51. however.   The character in this case is 'B' which is being treated as
  52. a memory location for a string.  What is there at virtual address
  53. 0x00000000b is anybodys guess, but if its unused .bss or .data space
  54. somewhere it would be zeroed on a UNIX system.
  55.  
  56. My guess is that %s in the format specifier is a typo, (which should
  57. have been a %c) either in the original or it got changed in typsetting.
  58.  
  59. :abc ABC GH <-----  
  60.  
  61. [...] 
  62.  
  63. This aint right, I think.  It should be GHB, (or if both the format string
  64. and the expression are screwed up in typesetting, the expression should
  65. be *s + 8 to give GHI).  All this is guessing, since I don't have the
  66. book, to get any idea of the context.  For all I know, the example is
  67. SUPPOSED to fail...  I did try the code example several ways and I
  68. really think that last %s should be a %c.
  69.  
  70. [...]
  71.  
  72. :    Which compiler are you using?
  73.  
  74. Plain ol' vanilla real (AT&T's K&R) compiler.  :-)
  75.  
  76. :    Milton Ribeiro (ribeiro@prl.philips.nl)
  77.  
  78.  
  79. -- 
  80. pat@rwing.uucp                                  (Pat Myrto),  Seattle, WA
  81.                       If all else fails, try:
  82.          ...!uunet!{pilchuck, polari}!rwing!pat
  83. WISDOM:    "Travelling unarmed is like boating without a life jacket" 
  84.