mastering pointers

I

iC and iC++

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.
 
B

Ben Bacarisse

iC and iC++ said:
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.
 
P

Peter Nilsson

iC and iC++ said:
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.
 
D

Dann Corbit

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


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.
 
S

Seebs

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
 
K

Keith Thompson

iC and iC++ said:
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.
 
D

Default User

iC and iC++ said:
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
 
P

Paul N

#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"...
 
J

John Bode

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.
 
R

Richard Bos

iC and iC++ said:
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
 
C

Chad

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.
 
I

iC and iC++

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.
 
C

Chad

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).
 
I

iC and iC++

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.
 
C

Chad

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.
 
R

Richard Bos

Chad said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top