home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!nwnexus!beauty!rwing!pat
- From: pat@rwing.UUCP (Pat Myrto)
- Newsgroups: comp.lang.c
- Subject: Re: ...so I typed this in straight from the book...
- Message-ID: <1836@rwing.UUCP>
- Date: 12 Dec 92 05:57:59 GMT
- References: <1g108jINNdg8@terminator.rs.itd.umich.edu> <1992Dec8.153936.15082@prl.philips.nl>
- Organization: Totally Unorganized
- Lines: 73
-
- In article <1992Dec8.153936.15082@prl.philips.nl> ribeiro@prl.philips.nl (ribeiro ma) writes:
- :In article <1g108jINNdg8@terminator.rs.itd.umich.edu> colinj@monet.ccs.itd.umich.edu (Colin Eric Johnson) writes:
- :>I typed this in straight in from the Kelly & Pohl book and it
- :>dumps core every time I try to run it. Lint Hates it (says
- :>main returns a random value).
- :>
- :>What's up with that?
- :>---------------------
- :>
- :>#include <stdio.h>
- :>#include <string.h>
- :>
- :>#define MAXSTRING 100
- :>
- :>main()
- :>{
- :> char c = 'a', *p, s[MAXSTRING];
- :>
- :> p = &c;
- :> printf("%c%c%c ", *p, *p + 1, *p + 2);
- :> strcpy(s, "ABC");
- :> printf("%s %c%c%s\n", s, *s + 6, *s + 7, *s + 1);
- :> strcpy(s, "she sells sea shell by the seashore");
- :> p = s + 14;
- :> for ( ; *p != '\0'; ++p) {
- :> if (*p == 'e')
- :> *p = 'E';
- :> if (*p == ' ')
- :> *p = '\n';
- :> }
- :> printf("%s\n", s);
- :>}
- : This piece of code compiles and runs fine with the standard cc equaliser
- :available with version 8.05 of HPUX. The output it gives is:
-
- I tried it and it ran for me, too, but that doesn't mean the code is right.
-
- I think that is just luck of how memory is laid out that it ran at all.
- The last expression in the printf() stmt under question (that is,
- '*s + 1') evaluates to a CHARACTER. The format specifier is for a STRING,
- however. The character in this case is 'B' which is being treated as
- a memory location for a string. What is there at virtual address
- 0x00000000b is anybodys guess, but if its unused .bss or .data space
- somewhere it would be zeroed on a UNIX system.
-
- My guess is that %s in the format specifier is a typo, (which should
- have been a %c) either in the original or it got changed in typsetting.
-
- :abc ABC GH <-----
-
- [...]
-
- This aint right, I think. It should be GHB, (or if both the format string
- and the expression are screwed up in typesetting, the expression should
- be *s + 8 to give GHI). All this is guessing, since I don't have the
- book, to get any idea of the context. For all I know, the example is
- SUPPOSED to fail... I did try the code example several ways and I
- really think that last %s should be a %c.
-
- [...]
-
- : Which compiler are you using?
-
- Plain ol' vanilla real (AT&T's K&R) compiler. :-)
-
- : Milton Ribeiro (ribeiro@prl.philips.nl)
-
-
- --
- pat@rwing.uucp (Pat Myrto), Seattle, WA
- If all else fails, try:
- ...!uunet!{pilchuck, polari}!rwing!pat
- WISDOM: "Travelling unarmed is like boating without a life jacket"
-