mastering pointers

Discussion in 'C Programming' started by iC and iC++, Jun 4, 2010.

  1. iC and iC++

    iC and iC++ Guest

    This is a continuation of my previous post where I am reviewing/
    learning from the book Mastering C Pointers: Tools for programming
    power by Robert J. > Traister.. 1990

    There is another piece of code in the book that I would like some
    clarification in:


    #include <stdio.h>
    int main (void)
    {
    char *a;
    a = "ANGULAR";
    *a = 66;
    printf("%s\n", a);
    }

    Now, this is in a section demonstrating a "poke" operation in C.

    He claims that this would return "BANGULAR", replacing the first
    character in the string "ANGULAR" with B, as it should.

    This compiles, but throws an error during execution. The question is,
    where is the error coming from.
     
    iC and iC++, Jun 4, 2010
    #1
    1. Advertising

  2. "iC and iC++" <> writes:

    > This is a continuation of my previous post where I am reviewing/
    > learning from the book Mastering C Pointers: Tools for programming
    > power by Robert J. > Traister.. 1990


    "This is not a book to be tossed aside lightly. It should be thrown
    with great force" (to paraphrase Dorothy Parker).

    > There is another piece of code in the book that I would like some
    > clarification in:
    >
    > #include <stdio.h>
    > int main (void)
    > {
    > char *a;
    > a = "ANGULAR";
    > *a = 66;
    > printf("%s\n", a);
    > }
    >
    > Now, this is in a section demonstrating a "poke" operation in C.
    >
    > He claims that this would return "BANGULAR", replacing the first
    > character in the string "ANGULAR" with B, as it should.
    >
    > This compiles, but throws an error during execution. The question is,
    > where is the error coming from.


    String literals are not modifiable. A program that attempts to write to
    the array that such a string literal give rise to has strayed into the
    realm of undefined behaviour. Any result is permitted but a run-time
    exception is particularly helpful.

    In fairness to the author, the 1990 edition does not claim to cover ANSI
    C. This program should have been corrected in the second edition which
    does, I think, make that claim.

    --
    Ben.
     
    Ben Bacarisse, Jun 4, 2010
    #2
    1. Advertising

  3. "iC and iC++" <> wrote:
    > There is another piece of code in the book that I would like some
    > clarification in:
    > ...
    >         char *a;
    >         a = "ANGULAR";
    >         *a = 66;


    That's a FAQ.

    --
    Peter
     
    Peter Nilsson, Jun 4, 2010
    #3
  4. iC and iC++

    iC and iC++ Guest


    > That's a FAQ.
    >
    > --
    > Peter


    I checked the FAQ before I posted but I must have missed it. I will
    check again, thanks.
     
    iC and iC++, Jun 4, 2010
    #4
  5. iC and iC++

    Dann Corbit Guest

    In article
    <>,
    says...
    >
    > "iC and iC++" <> writes:
    >
    > > This is a continuation of my previous post where I am reviewing/
    > > learning from the book Mastering C Pointers: Tools for programming
    > > power by Robert J. > Traister.. 1990

    >
    > "This is not a book to be tossed aside lightly. It should be thrown
    > with great force" (to paraphrase Dorothy Parker).
    >
    > > There is another piece of code in the book that I would like some
    > > clarification in:
    > >
    > > #include <stdio.h>
    > > int main (void)
    > > {
    > > char *a;
    > > a = "ANGULAR";
    > > *a = 66;
    > > printf("%s\n", a);
    > > }
    > >
    > > Now, this is in a section demonstrating a "poke" operation in C.
    > >
    > > He claims that this would return "BANGULAR", replacing the first
    > > character in the string "ANGULAR" with B, as it should.
    > >
    > > This compiles, but throws an error during execution. The question is,
    > > where is the error coming from.

    >
    > String literals are not modifiable. A program that attempts to write to
    > the array that such a string literal give rise to has strayed into the
    > realm of undefined behaviour. Any result is permitted but a run-time
    > exception is particularly helpful.
    >
    > In fairness to the author, the 1990 edition does not claim to cover ANSI
    > C. This program should have been corrected in the second edition which
    > does, I think, make that claim.


    http://groups.google.com/group/comp.lang.c/search?q=Traister

    Shortly, I expect spinoza to champion the Traister cause as a
    misunderstood genius.
     
    Dann Corbit, Jun 4, 2010
    #5
  6. iC and iC++

    Seebs Guest

    On 2010-06-04, Dann Corbit <> wrote:
    > Shortly, I expect spinoza to champion the Traister cause as a
    > misunderstood genius.


    This sounds like it has humor potential. Someone send me a copy of some
    edition so I can do a page about it, forget about it for a decade, and then
    have Hilarious Hijinx.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Jun 4, 2010
    #6
  7. "iC and iC++" <> writes:
    >> That's a FAQ.

    >
    > I checked the FAQ before I posted but I must have missed it. I will
    > check again, thanks.


    8.5, in the "Characters and strings" section.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 4, 2010
    #7
  8. iC and iC++

    Default User Guest

    "iC and iC++" <> wrote in message
    news:...
    >
    > This is a continuation of my previous post where I am reviewing/
    > learning from the book Mastering C Pointers: Tools for programming
    > power by Robert J. > Traister.. 1990


    Weren't you already advised not to use this book because it's full of
    errors? Are you on a mission to identify all those errors?



    Brian
     
    Default User, Jun 4, 2010
    #8
  9. iC and iC++

    Paul N Guest

    On 4 June, 01:59, "iC and iC++" <> wrote:
    > #include <stdio.h>
    > int main (void)
    > {
    >         char *a;
    >         a = "ANGULAR";
    >         *a = 66;
    >         printf("%s\n", a);
    >
    > }
    >
    > He claims that this would return "BANGULAR", replacing the first
    > character in the string "ANGULAR" with B, as it should.
    >
    > This compiles, but throws an error during execution. The question is,
    > where is the error coming from.


    In addition to the point others have made, it's perhaps worth pointing
    out another problem with the code. It uses the assumption that 66 is
    the code for "B", which is true on many computers but not on all. For
    portability you would be better saying 'B', ie a B in single quotes.

    Plus, of course, if it works it should say "BNGULAR" not "BANGULAR"...
     
    Paul N, Jun 4, 2010
    #9
  10. iC and iC++

    John Bode Guest

    On Jun 3, 7:59 pm, "iC and iC++" <> wrote:
    > This is a continuation of my previous post where I am reviewing/
    > learning from the book Mastering C Pointers: Tools for programming
    > power by Robert J.  > Traister.. 1990
    >
    > There is another piece of code in the book that I would like some
    > clarification in:
    >
    > #include <stdio.h>
    > int main (void)
    > {
    >         char *a;
    >         a = "ANGULAR";
    >         *a = 66;
    >         printf("%s\n", a);
    >
    > }
    >


    Bad juju -- attempting to modify the contents of a string literal
    invokes undefined behavior.

    Note that if you change this to

    char a[] = "ANGULAR";
    *a = 66;

    then it should work fine (for suitably loose definitions of "fine";
    this code assumes ASCII encoding); instead of manipulating the string
    literal, you are manipulating the contents of a local array with auto
    extent.

    String literals are arrays of char with static extent (meaning the
    memory for them is managed differently than memory for local
    variables). Depending on the specific platform, the memory containing
    a string literal may or may not be writable; since the C standard
    leaves the result undefined, it's safest to assume they aren't
    writable.
     
    John Bode, Jun 4, 2010
    #10
  11. iC and iC++

    Richard Bos Guest

    "iC and iC++" <> wrote:

    > This is a continuation of my previous post where I am reviewing/
    > learning from the book Mastering C Pointers: Tools for programming
    > power by Robert J. > Traister.. 1990


    Throw this book away.

    > char *a;
    > a = "ANGULAR";
    > *a = 66;


    Throw this book away. Twice.

    > printf("%s\n", a);
    > }
    >
    > Now, this is in a section demonstrating a "poke" operation in C.


    Throw this book away.

    > He claims that this would return "BANGULAR", replacing the first
    > character in the string "ANGULAR" with B, as it should.


    Throw this book away. Twice.

    > This compiles, but throws an error during execution. The question is,
    > where is the error coming from.


    The error is coming from the book being good only as fiber stuffing for
    your compost heap.

    I cannot repeat this enough: throw this book away.

    Richard
     
    Richard Bos, Jun 5, 2010
    #11
  12. iC and iC++

    Chad Guest

    On Jun 4, 12:21 pm, "Default User" <> wrote:
    > "iC and iC++" <> wrote in messagenews:...
    >
    >
    >
    > > This is a continuation of my previous post where I am reviewing/
    > > learning from the book Mastering C Pointers: Tools for programming
    > > power by Robert J.  > Traister.. 1990

    >
    > Weren't you already advised not to use this book because it's full of
    > errors? Are you on a mission to identify all those errors?
    >


    I would like to point out that there are some "slow" people that come
    around here. Seriously. Not all of us have the scholastic aptitude to
    get into a big name school right after high school. Some of us have to
    have to work just to get to the point of being slightly below average.
     
    Chad, Jun 5, 2010
    #12
  13. iC and iC++

    iC and iC++ Guest

    > Some of us have to have to work just to get to the
    > point of being slightly below average.


    I am sure thats where you are right now. Keep it up and maybe one day,
    you will become average, you dimwit.
     
    iC and iC++, Jun 5, 2010
    #13
  14. iC and iC++

    Chad Guest

    On Jun 5, 1:45 pm, "iC and iC++" <> wrote:
    > > Some of us have to have to work just to get to the
    > >  point of being slightly below average.

    >
    > I am sure thats where you are right now. Keep it up and maybe one day,
    > you will become average, you dimwit.


    Unlike you, I've actually admitted to being slow on several different
    occasions here. And unlike you, I've actually managed to overcome the
    learning curve associated with pointers. Say what you want. Your
    comp.lang.c posts speak more loudly than your education, or lack
    thereof, or any kind of professional programming experience that you
    claim to have (had).
     
    Chad, Jun 5, 2010
    #14
  15. iC and iC++

    iC and iC++ Guest

    On Jun 5, 5:34 pm, Chad <> wrote:
    > On Jun 5, 1:45 pm, "iC and iC++" <> wrote:
    >
    > > > Some of us have to have to work just to get to the
    > > >  point of being slightly below average.

    >
    > > I am sure thats where you are right now. Keep it up and maybe one day,
    > > you will become average, you dimwit.

    >
    > Unlike you, I've actually admitted to being slow on several different
    > occasions here. And unlike you, I've actually managed to overcome the
    > learning curve associated with pointers. Say what you want. Your
    > comp.lang.c posts speak more loudly than your education, or lack
    > thereof, or any kind of professional programming experience that you
    > claim to have (had).


    a. I am not a professional programmer.
    b. You need to learn to respect people if you want respect from
    others.
     
    iC and iC++, Jun 5, 2010
    #15
  16. iC and iC++

    Chad Guest

    On Jun 5, 2:56 pm, "iC and iC++" <> wrote:
    > On Jun 5, 5:34 pm, Chad <> wrote:
    >
    > > On Jun 5, 1:45 pm, "iC and iC++" <> wrote:

    >
    > > > > Some of us have to have to work just to get to the
    > > > >  point of being slightly below average.

    >
    > > > I am sure thats where you are right now. Keep it up and maybe one day,
    > > > you will become average, you dimwit.

    >
    > > Unlike you, I've actually admitted to being slow on several different
    > > occasions here. And unlike you, I've actually managed to overcome the
    > > learning curve associated with pointers. Say what you want. Your
    > > comp.lang.c posts speak more loudly than your education, or lack
    > > thereof, or any kind of professional programming experience that you
    > > claim to have (had).

    >
    > a. I am not a professional programmer.
    > b. You need to learn to respect people if you want respect from
    > others.


    You've done nothing to earn my respect. With that, I'm now putting you
    in my kill file.
     
    Chad, Jun 5, 2010
    #16
  17. iC and iC++

    Seebs Guest

    On 2010-06-05, iC and iC++ <> wrote:

    >> On Jun 5, 1:45 pm, "iC and iC++" <> wrote:
    >> > I am sure thats where you are right now. Keep it up and maybe one day,
    >> > you will become average, you dimwit.


    > b. You need to learn to respect people if you want respect from
    > others.


    Right you are. *plonk*

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Jun 6, 2010
    #17
  18. iC and iC++

    Richard Bos Guest

    Chad <> wrote:

    > On Jun 4, 12:21=A0pm, "Default User" <> wrote:
    > > "iC and iC++" <> wrote in messagenews:fce7849c-b849-45ea=


    > > > This is a continuation of my previous post where I am reviewing/
    > > > learning from the book Mastering C Pointers: Tools for programming
    > > > power by Robert J. =A0> Traister.. 1990

    > >
    > > Weren't you already advised not to use this book because it's full of
    > > errors? Are you on a mission to identify all those errors?

    >
    > I would like to point out that there are some "slow" people that come
    > around here. Seriously. Not all of us have the scholastic aptitude to
    > get into a big name school right after high school. Some of us have to
    > have to work just to get to the point of being slightly below average.


    All the more reason not to use misleading text books, and to take the
    advice of those who _do_ know the language inside-out.

    Richard
     
    Richard Bos, Jun 6, 2010
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maniac

    "Mastering C Pointers"....

    maniac, Nov 1, 2003, in forum: C Programming
    Replies:
    388
    Views:
    9,409
    Mark McIntyre
    Nov 18, 2003
  2. Moritz Beller

    O'Reilly book: Mastering algorithms with C

    Moritz Beller, Jun 4, 2004, in forum: C Programming
    Replies:
    7
    Views:
    1,416
    CBFalconer
    Jun 7, 2004
  3. moonhk
    Replies:
    3
    Views:
    492
    moonhk
    Nov 8, 2006
  4. Gerald

    Mastering Java

    Gerald, Mar 16, 2007, in forum: Java
    Replies:
    6
    Views:
    384
  5. Gerald

    Mastering Python

    Gerald, Mar 16, 2007, in forum: Python
    Replies:
    32
    Views:
    2,623
    Hendrik van Rooyen
    Apr 3, 2007
Loading...

Share This Page