Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > mastering pointers

Reply
Thread Tools

mastering pointers

 
 
iC and iC++
Guest
Posts: n/a
 
      06-04-2010

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.

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-04-2010
"iC and iC++" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      06-04-2010
"iC and iC++" <(E-Mail Removed)> 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
 
Reply With Quote
 
iC and iC++
Guest
Posts: n/a
 
      06-04-2010

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


I checked the FAQ before I posted but I must have missed it. I will
check again, thanks.
 
Reply With Quote
 
Dann Corbit
Guest
Posts: n/a
 
      06-04-2010
In article
<0.188fbb8a2fb0980bea12.20100604022133BST.87ocfrsi (E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) says...
>
> "iC and iC++" <(E-Mail Removed)> 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....rch?q=Traister

Shortly, I expect spinoza to champion the Traister cause as a
misunderstood genius.

 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      06-04-2010
On 2010-06-04, Dann Corbit <(E-Mail Removed)> 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 / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-04-2010
"iC and iC++" <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      06-04-2010

"iC and iC++" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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


 
Reply With Quote
 
Paul N
Guest
Posts: n/a
 
      06-04-2010
On 4 June, 01:59, "iC and iC++" <(E-Mail Removed)> 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"...

 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      06-04-2010
On Jun 3, 7:59*pm, "iC and iC++" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Mastering the Nikon D70 jkissing Digital Photography 6 05-11-2004 07:51 PM
Mastering Microsoft Visual J++ 6 Steve MCSD 1 04-05-2004 11:54 AM
"Mastering C Pointers".... maniac C Programming 388 11-18-2003 11:15 PM
Mastering Visual Basic 6.0 Problem Programmer Microsoft Certification 0 11-13-2003 10:33 PM



Advertisments