473,396 Members | 1,996 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,396 software developers and data experts.

Linked list: casting a malloc pointer

I'm referring to linked list.

In some codes I find:

/* 1 */
newNode=malloc(sizeof (struct* node));

in other ones, instead I find:

/* 2 */
newNode (list_type) malloc (sizeof (struct* node));

Which one is correct?
Nov 15 '05 #1
3 1501
__frank__ a écrit :
I'm referring to linked list.

In some codes I find:

/* 1 */
newNode=malloc(sizeof (struct* node));

in other ones, instead I find:

/* 2 */
newNode (list_type) malloc (sizeof (struct* node));

Which one is correct?

None of them (for the same reason : WtH is 'struct* node' ?)

Try this one :

struct node * newNode = malloc (sizeof *newNode);

and be sure to include <stdlib.h>

Also, test the value returned by malloc(), and free() when not used anymore.
Nov 15 '05 #2
__frank__ wrote:
I'm referring to linked list.

In some codes I find:

/* 1 */
newNode=malloc(sizeof (struct* node));
This function allocates only enough space for the pointer of the node.
Is this what you want, or do you want to allocate enough space for the
entire node?

in other ones, instead I find:

/* 2 */
newNode (list_type) malloc (sizeof (struct* node));
This is not grammatically correct. It would be much better if you could
actually copy and paste the relevant sections of code. Otherwise we're
just guessing.

As mentioned previously in this newsgroup (read back up to a couple of
weeks), you will find many mentions about your question. Don't typecast
the result of malloc(). See the C newsgroup faq and previous posts about
why.

Which one is correct?
From what we have, the first one is better. But with no code posted,
both could be wrong.

It would be most helpful if you could post a minimal sample in C that
can be compiled (that you have also personally compiled) that
demonstrates what you are asking for.

Nov 15 '05 #3
__frank__ wrote:

I'm referring to linked list.

In some codes I find:

/* 1 */
newNode=malloc(sizeof (struct* node));

in other ones, instead I find:

/* 2 */
newNode (list_type) malloc (sizeof (struct* node));

Which one is correct?


If you have included the proper header files, then both of the above
(once you fix the missing '=' for #2) are equivalent.

However...

You probably mean "sizeof(struct node)", and "sizeof(*newNode)" is even
better, as newNode's type can change and the malloc will automatically
get the right size.

And, if you don't remember to include the proper header file, then #2
will mask the error, so most people here will tell you the #1 is "better".

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 15 '05 #4

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

Similar topics

10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
10
by: Aditya | last post by:
Hi All, I would like to know how it is possible to insert a node in a linked list without using a temp_pointer. If the element is the first element then there is no problem but if it is in...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.