473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help with understanding this please

mdh
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 :-)

Aug 30 '08 #1
19 1318
mdh said:
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.
/* error: initializer element is not constant */
Yes. You can't call a function except from within a function.
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
Aug 30 '08 #2
mdh
>
Hmmm. I think you've been awake far too long. Get some rest. :-)


:-) I think you are correct

Aug 30 '08 #3
mdh
On Aug 30, 7:11*am, Richard Heathfield <r...@see.sig.invalidwrote:
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.

Aug 30 '08 #4
mdh
On Aug 30, 7:11 am, Richard Heathfield <r...@see.sig.invalidwrote:
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.
Aug 30 '08 #5
mdh said:
On Aug 30, 7:11 am, Richard Heathfield <r...@see.sig.invalidwrote:
>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?
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
Aug 30 '08 #6
mdh
On Aug 30, 7:36*am, Richard Heathfield <r...@see.sig.invalidwrote:
>
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.

Aug 30 '08 #7
Richard Heathfield <rj*@see.sig.invalidwrites:
mdh said:
>On Aug 30, 7:11 am, Richard Heathfield <r...@see.sig.invalidwrote:
>>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.
Aug 30 '08 #8
"Richard" <rg****@gmail.comwrote in message news:
Richard Heathfield <rj*@see.sig.invalidwrites:
>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.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 30 '08 #9
"Malcolm McLean" <re*******@btinternet.comwrites:
"Richard" <rg****@gmail.comwrote in message news:
>Richard Heathfield <rj*@see.sig.invalidwrites:
>>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.
Aug 30 '08 #10
mdh <md**@comcast.netwrites:
[...]
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) ks***@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"
Aug 30 '08 #11
mdh <md**@comcast.netwrote:
>
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
Aug 30 '08 #12
Richard <rg****@gmail.comwrote [re. casting the return value of malloc]:
Richard Heathfield <rj*@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.
--
Larry Jones

Everything's gotta have rules, rules, rules! -- Calvin
Aug 30 '08 #13
la************@siemens.com writes:
Richard <rg****@gmail.comwrote [re. casting the return value of malloc]:
>Richard Heathfield <rj*@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.
Aug 30 '08 #14
mdh wrote:
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.

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.
Aug 30 '08 #15
mdh
On Aug 30, 4:21*pm, Mark McIntyre <markmcint...@TROUSERSspamcop.net>
wrote:
mdh wrote:
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.
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.
Aug 30 '08 #16
Richard<rg****@gmail.comwrites:
la************@siemens.com writes:
>Richard <rg****@gmail.comwrote [re. casting the return value of malloc]:
>>Richard Heathfield <rj*@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) ks***@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"
Aug 31 '08 #17
Mark McIntyre said:
mdh wrote:
>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.

>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
Aug 31 '08 #18
Keith Thompson <ks***@mib.orgwrites:
Richard<rg****@gmail.comwrites:
>la************@siemens.com writes:
>>Richard <rg****@gmail.comwrote [re. casting the return value of malloc]:
Richard Heathfield <rj*@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
>>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.
Aug 31 '08 #19
Richard<rg****@gmail.comwrites:
Keith Thompson <ks***@mib.orgwrites:
>Richard<rg****@gmail.comwrites:
>>la************@siemens.com writes:
Richard <rg****@gmail.comwrote [re. casting the return value of malloc]:
Richard Heathfield <rj*@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.
If you think
>>>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) ks***@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"
Aug 31 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: KL | last post by:
I am writing this function and I think I messed up something cause it won't work. Can someone help me see what I am doing wrong?? I have included the function and the part where I call the...
5
by: Beringer | last post by:
I am trying to access some functionality of the underlying RichTextBox control and am getting all mixed up with conversions of types. I know my basic issue is not completely understanding...
2
by: SStory | last post by:
Here is the situation. I want to display Icons, Type of file etc from a file extension. Upon initial program load I may only need icons for certain files. But other operations will require...
40
by: apprentice | last post by:
Hello, I'm writing an class library that I imagine people from different countries might be interested in using, so I'm considering what needs to be provided to support foreign languages,...
4
by: joelagnel | last post by:
I'm new to .NET and have a basic understanding of operating systems. I can also do some magic in Csharp, but I lack an understanding of what's going on under the hood, with respect to how windows...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
19
by: mohammaditraders | last post by:
a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions Cal_percentage() which calculate the...
6
by: chris.kemmerer | last post by:
I am having a problem with templates and I hope someone here can help. I am writing a library that accepts data packets, parses them and saves the information for later use. One member of the...
2
by: =?Utf-8?B?U2NvdHRSYWREZXY=?= | last post by:
I'm creating a doc project for my c# program. I've done this before but this time sonething is wrong. I build my doc project and is succeeds but when I open the help file, there is no documentation...
110
by: fjm | last post by:
For some reason, I have always had a hard time understanding arrays as they pertain to php and databases. I understand associative arrays just fine but when there are multidimensional arrays, I kinda...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.