tree example

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

Can anyone tell me what the (char *) is on page 143 kandr2 ? iinning to
understand the tree example but no what I just memtioned.

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
Can anyone tell me what the (char *) is on page 143 kandr2 ? iinning to
understand the tree example but no what I just memtioned.

How big a bit of code is it? Is it worth limiting the replies to
people who have K&R2 to hand (I don't) to save typing it in?
 
O

osmium

Bill Cunningham said:
Can anyone tell me what the (char *) is on page 143 kandr2 ? iinning to
understand the tree example but no what I just memtioned.

It's a _cast_. Casts are discussed on p 45 of K&R.
 
B

Barry Schwarz

Can anyone tell me what the (char *) is on page 143 kandr2 ? iinning to
understand the tree example but no what I just memtioned.

The only occurrence of the string "(char *)" in page 143 is a cast
applied to the return value of malloc. In the errata to K&R II, they
acknowledge that malloc and it siblings do not need this type of cast.
This code is a carry over from the pre-ANSI days when malloc returned
a char*.

This entire block of code is an implementation of the strdup function
which is used on page 141. Other than the fact that the addtree
function calls strdup, the function is not related to trees at all. It
merely copies the 'source' string (whose address is passed in an
argument) into a dynamically allocated block of memory.
 
B

Bill Cunningham

Barry Schwarz said:
The only occurrence of the string "(char *)" in page 143 is a cast
applied to the return value of malloc. In the errata to K&R II, they
acknowledge that malloc and it siblings do not need this type of cast.
This code is a carry over from the pre-ANSI days when malloc returned
a char*.

This entire block of code is an implementation of the strdup function
which is used on page 141. Other than the fact that the addtree
function calls strdup, the function is not related to trees at all. It
merely copies the 'source' string (whose address is passed in an
argument) into a dynamically allocated block of memory.

This is exactly why I have never got into casting. I've always been told
that they aren't needed. If not at all only very rare exceptions so it duped
me.

Bill
 
B

Bill Cunningham

It's a _cast_. Casts are discussed on p 45 of K&R.

That's why I never got into casting. I've always been told it isn't
needed. It duped me as you can see from the posted below.

Bill
 
P

Phil Carmody

Bill Cunningham said:
That's why I never got into casting. I've always been told it isn't
needed. It duped me as you can see from the posted below.

It's almost only ever *needed*. It's almost never *wanted*.

Phil
 

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

tree 13
Good book 34
tree 8
tree 12
iteration 4
Copy string from 2D array to a 1D array in C 1
tree errors 95
tdelete() example? 1

Members online

No members online now.

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,371
Latest member
TroyHursey

Latest Threads

Top