Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » CGoto page 1, 2  Next

Help with understanding this please

 
Jump to:  
 
mdh
PostPosted: Sat Aug 30, 2008 2:03 pm    Post subject: 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 Smile
 

 
Richard Heathfield
PostPosted: Sat Aug 30, 2008 2:03 pm    Post subject: 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
 

 
Richard Heathfield
PostPosted: Sat Aug 30, 2008 2:03 pm    Post subject: Re: Help with understanding this please
       
mdh said:

Quote:
On Aug 30, 7:11 am, Richard Heathfield <r...@see.sig.invalid> wrote:
mdh said:


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?

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.h> for 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
 

 
Richar
PostPosted: Sat Aug 30, 2008 2:03 pm    Post subject: Re: Help with understanding this please
       
Richard Heathfield <rjh@see.sig.invalid> writes:

Quote:
mdh said:

On Aug 30, 7:11 am, Richard Heathfield <r...@see.sig.invalid> wrote:
mdh said:


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.
 

 
Malcolm McLean
PostPosted: Sat Aug 30, 2008 2:03 pm    Post subject: Re: Help with understanding this please
       
"Richard" <rgrdev@gmail.com> wrote in message news:
Quote:
Richard Heathfield <rjh@see.sig.invalid> writes:

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.


--
Free games and programming goodies.
LINK
 

 
Richar
PostPosted: Sat Aug 30, 2008 2:03 pm    Post subject: Re: Help with understanding this please
       
"Malcolm McLean" <regniztar@btinternet.com> writes:

Quote:
"Richard" <rgrdev@gmail.com> wrote in message news:
Richard Heathfield <rjh@see.sig.invalid> writes:

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.
 

 
mdh
PostPosted: Sat Aug 30, 2008 2:11 pm    Post subject: Re: Help with understanding this please
       
Quote:

Hmmm. I think you've been awake far too long. Get some rest. Smile



:-) I think you are correct
 

 
mdh
PostPosted: Sat Aug 30, 2008 2:22 pm    Post subject: Re: Help with understanding this please
       
On Aug 30, 7:11 am, Richard Heathfield <r...@see.sig.invalid> wrote:
Quote:
mdh said:


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.
 

 
mdh
PostPosted: Sat Aug 30, 2008 2:30 pm    Post subject: Re: Help with understanding this please
       
On Aug 30, 7:11 am, Richard Heathfield <r...@see.sig.invalid> wrote:
Quote:
mdh said:
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.
 

 
mdh
PostPosted: Sat Aug 30, 2008 2:38 pm    Post subject: Re: Help with understanding this please
       
On Aug 30, 7:36 am, Richard Heathfield <r...@see.sig.invalid> wrote:
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?

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.
 

Page 1 of 2 .:. Goto page 1, 2  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

lokata baseny szukaj mp3 Parkiet Hale stalowe