Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 30th, 2008, 03:05 PM
mdh
Guest
 
Posts: n/a
Default Help with understanding this please

I am trying to define a struct called temp, using this code. Please be
kind in explaining with this is not working :-)

#include <stdio.h>
#include <stdlib.h>


struct t_node_n{
char *word;
int match;
struct t_node_n *left;
struct t_node_n *right;
};


struct t_node_n *temp = (struct t_node_n *) malloc(sizeof(struct
t_node_n)); /* error: initializer element is not constant */

int main {};



So, this is what I **thought** I was doing.
Create a pointer of type struct t_node_n called "temp".
Define the pointer "temp" by calling malloc....which I **thought**
gives the pointer "memory" to point to, thus defines it?
Clearly these thoughts are way off base!!


Thanks :-)





  #2  
Old August 30th, 2008, 03:15 PM
Richard Heathfield
Guest
 
Posts: n/a
Default Re: Help with understanding this please

mdh said:
Quote:
I am trying to define a struct called temp, using this code. Please be
kind in explaining with this is not working :-)
>
#include <stdio.h>
#include <stdlib.h>
>
>
struct t_node_n{
char *word;
int match;
struct t_node_n *left;
struct t_node_n *right;
};
>
>
struct t_node_n *temp = (struct t_node_n *) malloc(sizeof(struct
t_node_n));
Better: struct t_node_n *temp = malloc(sizeof *temp);

Cleaner, tighter, less maintenance hassle.
Quote:
/* error: initializer element is not constant */
Yes. You can't call a function except from within a function.
Quote:
int main {};
Hmmm. I think you've been awake far too long. Get some rest. :-)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  #3  
Old August 30th, 2008, 03:15 PM
mdh
Guest
 
Posts: n/a
Default Re: Help with understanding this please

Quote:
>
Hmmm. I think you've been awake far too long. Get some rest. :-)


:-) I think you are correct

  #4  
Old August 30th, 2008, 03:25 PM
mdh
Guest
 
Posts: n/a
Default Re: Help with understanding this please

On Aug 30, 7:11*am, Richard Heathfield <r...@see.sig.invalidwrote:
Quote:
mdh said:
Quote:
>
Quote:
struct t_node_n{
char *word;
int match;
struct t_node_n *left;
struct t_node_n *right;
};
>
Quote:
struct t_node_n *temp = (struct t_node_n *) malloc(sizeof(struct
t_node_n));
>
Better: struct t_node_n *temp = malloc(sizeof *temp);
>
Cleaner, tighter, less maintenance hassle.
Richard...before I rest so that sleep can be calm!!, may I ask why you
have not cast the return from malloc to type "struct t_node n *). K&R
devote quite a discussion to this on page 142.

Thanks as always.

  #5  
Old August 30th, 2008, 03:35 PM
mdh
Guest
 
Posts: n/a
Default Re: Help with understanding this please

On Aug 30, 7:11 am, Richard Heathfield <r...@see.sig.invalidwrote:
Quote:
mdh said:
Quote:
struct t_node_n{
char *word;
int match;
struct t_node_n *left;
struct t_node_n *right;
};
struct t_node_n *temp = (struct t_node_n *) malloc(sizeof(struct
t_node_n));
Better: struct t_node_n *temp = malloc(sizeof *temp);
Cleaner, tighter, less maintenance hassle.

Richard...before I rest so that sleep can be calm!!, may I ask why
you
have not cast the return from malloc to type (struct t_node n *). K&R
devote quite a discussion to this on page 142.
Thanks as always.
  #6  
Old August 30th, 2008, 03:35 PM
Richard Heathfield
Guest
 
Posts: n/a
Default Re: Help with understanding this please

mdh said:
Quote:
On Aug 30, 7:11 am, Richard Heathfield <r...@see.sig.invalidwrote:
Quote:
>mdh said:
>
Quote:
>>
Quote:
struct t_node_n{
char *word;
int match;
struct t_node_n *left;
struct t_node_n *right;
};
>>
Quote:
struct t_node_n *temp = (struct t_node_n *) malloc(sizeof(struct
t_node_n));
>>
>Better: struct t_node_n *temp = malloc(sizeof *temp);
>>
>Cleaner, tighter, less maintenance hassle.
>
Richard...before I rest so that sleep can be calm!!, may I ask why you
have not cast the return from malloc to type "struct t_node n *).
Why on earth would I do that? What good could it possibly do?
Quote:
K&R devote quite a discussion to this on page 142.
That *was* the proper method, before void * was introduced to the language.
This is one of those very few parts of this excellent book that should
have been written more carefully. It is true that the malloc function used
to return char *, way back in the Cretaceous Period, but for about twenty
years it has returned void *. The language automagically provides an
implicit conversion from void * to struct t_node_n *, so there is no need
for a cast. Given that it does no good, and stops nothing bad happening,
and can in fact obscure the accidental omission of <stdlib.hfor the
malloc prototype, I see no reason to include a cast and every reason not
to include one.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  #7  
Old August 30th, 2008, 03:45 PM
mdh
Guest
 
Posts: n/a
Default Re: Help with understanding this please

On Aug 30, 7:36*am, Richard Heathfield <r...@see.sig.invalidwrote:
Quote:
>
Quote:
Richard...before I rest so that sleep can be calm!!, may I ask why you
have not cast the return from malloc to type "struct t_node n *).
>
Why on earth would I do that? What good could it possibly do?
>
Quote:
K&R devote quite a discussion to this on page 142.
>
That *was* the proper method, before void * was introduced to the language.
This is one of those very few parts of this excellent book that should
have been written more carefully. It is true that the malloc function used
to return char *, way back in the Cretaceous Period, but for about twenty
years it has returned void *. The language automagically provides an
implicit conversion from void * to struct t_node_n *, so there is no need
for a cast.
Thanks as always...and thanks for the humor or humour...which I know
is lost on some, but is immensely appreciated by myself.



  #8  
Old August 30th, 2008, 04:25 PM
Richard
Guest
 
Posts: n/a
Default Re: Help with understanding this please

Richard Heathfield <rjh@see.sig.invalidwrites:
Quote:
mdh said:
>
Quote:
>On Aug 30, 7:11 am, Richard Heathfield <r...@see.sig.invalidwrote:
Quote:
>>mdh said:
>>
Quote:
>>>
>struct t_node_n{
>char *word;
>int match;
>struct t_node_n *left;
>struct t_node_n *right;
>};
>>>
>struct t_node_n *temp = (struct t_node_n *) malloc(sizeof(struct
>t_node_n));
>>>
>>Better: struct t_node_n *temp = malloc(sizeof *temp);
>>>
>>Cleaner, tighter, less maintenance hassle.
>>
>Richard...before I rest so that sleep can be calm!!, may I ask why you
>have not cast the return from malloc to type "struct t_node n *).
>
Why on earth would I do that? What good could it possibly do?
Yeah because C++ is wrong and so was pre Ansi C.

Streuth.
  #9  
Old August 30th, 2008, 04:35 PM
Malcolm McLean
Guest
 
Posts: n/a
Default Re: Help with understanding this please

"Richard" <rgrdev@gmail.comwrote in message news:
Quote:
Richard Heathfield <rjh@see.sig.invalidwrites:
>
Quote:
>mdh said:
>>
Quote:
>>Richard...before I rest so that sleep can be calm!!, may I ask why you
>>have not cast the return from malloc to type "struct t_node n *).
>>
>Why on earth would I do that? What good could it possibly do?
>
Yeah because C++ is wrong and so was pre Ansi C.
>
It's an emphasis problem.
You want to warn about potentially unsafe operations, such as converting
from one type to another.
However only rarely will you want to assign the result of malloc() to a void
*. In this case, the warnings are gratuitous. Too many warnings are as bad
as too few, because people start ignoring them, and the genuinely
exceptional casts get lost in the noise.


--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

  #10  
Old August 30th, 2008, 04:55 PM
Richard
Guest
 
Posts: n/a
Default Re: Help with understanding this please

"Malcolm McLean" <regniztar@btinternet.comwrites:
Quote:
"Richard" <rgrdev@gmail.comwrote in message news:
Quote:
>Richard Heathfield <rjh@see.sig.invalidwrites:
>>
Quote:
>>mdh said:
>>>
>>>Richard...before I rest so that sleep can be calm!!, may I ask why you
>>>have not cast the return from malloc to type "struct t_node n *).
>>>
>>Why on earth would I do that? What good could it possibly do?
>>
>Yeah because C++ is wrong and so was pre Ansi C.
>>
It's an emphasis problem.
You want to warn about potentially unsafe operations, such as
converting from one type to another.
However only rarely will you want to assign the result of malloc() to
a void *. In this case, the warnings are gratuitous. Too many warnings
are as bad as too few, because people start ignoring them, and the
genuinely exceptional casts get lost in the noise.
You miss the point. Heathfield's typically smarmy reply seemed to indicate
that the cast was something only an idiot would do.
  #11  
Old August 30th, 2008, 08:15 PM
Keith Thompson
Guest
 
Posts: n/a
Default Re: Help with understanding this please

mdh <mdeh@comcast.netwrites:
[...]
Quote:
Richard...before I rest so that sleep can be calm!!, may I ask why you
have not cast the return from malloc to type "struct t_node n *). K&R
devote quite a discussion to this on page 142.
[...]

It's also discussed on the K&R2 errata page,
<http://plan9.bell-labs.com/cm/cs/cbook/2ediffs.html>.
Now that the server is back up, I can quote it:

142(6.5, toward the end): The remark about casting the return
value of malloc ("the proper method is to declare ... then
explicitly coerce") needs to be rewritten. The example is correct
and works, but the advice is debatable in the context of the
1988-1989 ANSI/ISO standards. It's not necessary (given that
coercion of void * to ALMOSTANYTYPE * is automatic), and possibly
harmful if malloc, or a proxy for it, fails to be declared as
returning void *. The explicit cast can cover up an unintended
error. On the other hand, pre-ANSI, the cast was necessary, and it
is in C++ also.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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"
  #12  
Old August 30th, 2008, 09:15 PM
lawrence.jones@siemens.com
Guest
 
Posts: n/a
Default Re: Help with understanding this please

mdh <mdeh@comcast.netwrote:
Quote:
>
Richard...before I rest so that sleep can be calm!!, may I ask why you
have not cast the return from malloc to type (struct t_node n *). K&R
devote quite a discussion to this on page 142.
See the errata for that page:

<http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html>
--
Larry Jones

Shut up and go get me some antiseptic. -- Calvin
  #13  
Old August 30th, 2008, 09:15 PM
lawrence.jones@siemens.com
Guest
 
Posts: n/a
Default Re: Help with understanding this please

Richard <rgrdev@gmail.comwrote [re. casting the return value of malloc]:
Quote:
Richard Heathfield <rjh@see.sig.invalidwrites:
Quote:

Why on earth would I do that? What good could it possibly do?
>
Yeah because C++ is wrong and so was pre Ansi C.
C++ isn't wrong, it's different (C++ programs should use new rather than
malloc). Pre-ANSI C *was* wrong, that's why ANSI changed it.
--
Larry Jones

Everything's gotta have rules, rules, rules! -- Calvin
  #14  
Old August 30th, 2008, 09:45 PM
Richard
Guest
 
Posts: n/a
Default Re: Help with understanding this please

lawrence.jones@siemens.com writes:
Quote:
Richard <rgrdev@gmail.comwrote [re. casting the return value of malloc]:
Quote:
>Richard Heathfield <rjh@see.sig.invalidwrites:
Quote:
>
Why on earth would I do that? What good could it possibly do?
>>
>Yeah because C++ is wrong and so was pre Ansi C.
>
C++ isn't wrong, it's different (C++ programs should use new rather than
malloc). Pre-ANSI C *was* wrong, that's why ANSI changed it.
My point seems to have eluded you. And that is it is NOT obvious. And
Heathfield's reply was less than useless.
  #15  
Old August 31st, 2008, 12:25 AM
Mark McIntyre
Guest
 
Posts: n/a
Default Re: Help with understanding this please

mdh wrote:
Quote:
may I ask why you
have not cast the return from malloc to type "struct t_node n *).
In C, you *never* have to cast the return from malloc.

Quote:
K&R devote quite a discussion to this on page 142.
This is an error in K&R which the authors have acknowledged. The
discussion predates ANSI C.
  #16  
Old August 31st, 2008, 12:35 AM
mdh
Guest
 
Posts: n/a
Default Re: Help with understanding this please

On Aug 30, 4:21*pm, Mark McIntyre <markmcint...@TROUSERSspamcop.net>
wrote:
Quote:
mdh wrote:
Quote:
may I ask why you
have not cast the return from malloc to type "struct t_node n *).
>
In C, you *never* have to cast the return from malloc.
>
Quote:
K&R devote quite a discussion to this on page 142.
>
This is an error in K&R which the authors have acknowledged. The
discussion predates ANSI C.
Thank you to all who have pointed this out...much appreciated.
  #17  
Old August 31st, 2008, 02:15 AM
Keith Thompson
Guest
 
Posts: n/a
Default Re: Help with understanding this please

Richard<rgrdev@gmail.comwrites:
Quote:
lawrence.jones@siemens.com writes:
Quote:
>Richard <rgrdev@gmail.comwrote [re. casting the return value of malloc]:
Quote:
>>Richard Heathfield <rjh@see.sig.invalidwrites:
>>
>Why on earth would I do that? What good could it possibly do?
>>>
>>Yeah because C++ is wrong and so was pre Ansi C.
>>
>C++ isn't wrong, it's different (C++ programs should use new rather than
>malloc). Pre-ANSI C *was* wrong, that's why ANSI changed it.
>
My point seems to have eluded you. And that is it is NOT obvious. And
Heathfield's reply was less than useless.
Let me draw your attention to the original poster's response to
RH's "less than useless" reply:

Thanks as always...and thanks for the humor or humour...which I know
is lost on some, but is immensely appreciated by myself.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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"
  #18  
Old August 31st, 2008, 07:05 AM
Richard Heathfield
Guest
 
Posts: n/a
Default Re: Help with understanding this please

Mark McIntyre said:
Quote:
mdh wrote:
Quote:
>may I ask why you
>have not cast the return from malloc to type "struct t_node n *).
>
In C, you *never* have to cast the return from malloc.
>
>
Quote:
>K&R devote quite a discussion to this on page 142.
>
This is an error in K&R which the authors have acknowledged.
The discussion on p142 may be regarded as an error, yes. Note, however,
that the actual cast is not an error, merely poor style.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  #19  
Old August 31st, 2008, 11:25 AM
Richard
Guest
 
Posts: n/a
Default Re: Help with understanding this please

Keith Thompson <kst-u@mib.orgwrites:
Quote:
Richard<rgrdev@gmail.comwrites:
Quote:
>lawrence.jones@siemens.com writes:
Quote:
>>Richard <rgrdev@gmail.comwrote [re. casting the return value of malloc]:
>>>Richard Heathfield <rjh@see.sig.invalidwrites:
>>>
>>Why on earth would I do that? What good could it possibly do?
>>>>
>>>Yeah because C++ is wrong and so was pre Ansi C.
>>>
>>C++ isn't wrong, it's different (C++ programs should use new rather than
>>malloc). Pre-ANSI C *was* wrong, that's why ANSI changed it.
>>
>My point seems to have eluded you. And that is it is NOT obvious. And
>Heathfield's reply was less than useless.
>
Let me draw your attention to the original poster's response to
RH's "less than useless" reply:
>
Thanks as always...and thanks for the humor or humour...which I know
is lost on some, but is immensely appreciated by myself.
I'm not sure if you are trying to be amusing or typically pedantic and
elusive.

If you think
Quote:
Quote:
Quote:
>>Why on earth would I do that? What good could it possibly do?
is a good answer to a newbies question then you need to take a break.


  #20  
Old August 31st, 2008, 12:35 PM
Keith Thompson
Guest
 
Posts: n/a
Default Re: Help with understanding this please

Richard<rgrdev@gmail.comwrites:
Quote:
Keith Thompson <kst-u@mib.orgwrites:
Quote:
>Richard<rgrdev@gmail.comwrites:
Quote:
>>lawrence.jones@siemens.com writes:
>>>Richard <rgrdev@gmail.comwrote [re. casting the return value of malloc]:
>>>>Richard Heathfield <rjh@see.sig.invalidwrites:
>>>Why on earth would I do that? What good could it possibly do?
>>>>>
>>>>Yeah because C++ is wrong and so was pre Ansi C.
>>>>
>>>C++ isn't wrong, it's different (C++ programs should use new rather than
>>>malloc). Pre-ANSI C *was* wrong, that's why ANSI changed it.
>>>
>>My point seems to have eluded you. And that is it is NOT obvious. And
>>Heathfield's reply was less than useless.
>>
>Let me draw your attention to the original poster's response to
>RH's "less than useless" reply:
>>
> Thanks as always...and thanks for the humor or humour...which I know
> is lost on some, but is immensely appreciated by myself.
>
I'm not sure if you are trying to be amusing or typically pedantic and
elusive.
Neither.
Quote:
If you think
>
Quote:
Quote:
>>>Why on earth would I do that? What good could it possibly do?
>
is a good answer to a newbies question then you need to take a break.
I don't think it would have been a good answer by itself. It was, in
fact, a preamble to a longer answer (which you naturally snipped)
which explained why there is no good reason to cast the result of
malloc(). Since the person asking the question found the answer *as a
whole* useful, your opinion that it's "less than useless" would seem
to be nothing more than your usual trolling.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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"
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles