1

I am struggling with understanding structs and pointers, and thus, struggling to understand examples of linked lists in both my textbook and online.

What does the following code mean:

(struct NODE *)malloc(sizeof(struct NODE));

Will someone please provide a detailed explanation?

I understand that something is being allocated memory (the size in bytes of struct NODE), however, I don't understand what

(struct NODE *)

means.

stk
  • 13
  • 4
  • 3
    It's a type cast, to "pointer to `struct NODE`". Completely superfluous, the return value of `malloc` should not be cast. – Daniel Fischer Jul 17 '13 at 21:29
  • possible duplicate of [What are the barriers to understanding pointers and what can be done to overcome them?](http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome) – fvrghl Jul 17 '13 at 21:49

2 Answers2

2

Function malloc() returns address of allocated memory. Return type of malloc() function is void* (it don't know for which type of data you are allocating memory), to assign it to pointer of your structure type you typecasts it into required type. So in you expression (struct NODE *) is typecast instruction:

(struct NODE *) malloc (sizeof(struct NODE));
     ^              
     |             ^
      Typecast     | call of malloc function with argument = sizeof(struct NODE)

Generally, you should avoid typecast a returned value from malloc/calloc functions in C ( read this: Do I cast the result of malloc? )

In C syntax of typecast is:

 (rhsdatatype) data;

rhsdatatype should be in parenthesis before data.

Sometime in programming you need typecast: for example.

int a = 2; 
int b = 3; 
double c = a / b; 

This code outputs 0.0 because 2/3 both are integers to / result will be int that is 0, and you assigns to double variable c = 0. (what you may not wants).

So here typecast is solution, new code is:

int a = 2; 
int b = 3; 
double c = (double)a / (double)b; 

it outputs real number output that is: 0.666.

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • 2
    typecast, not typecase. –  Jul 17 '13 at 21:33
  • 1
    Thank you for your help! I appreciate your extra-detailed response. – stk Jul 17 '13 at 21:50
  • @GrijeshChauhan If I created a pointer-to-struct-node *p with the statement "struct node *p;" and then allocated memory for it with "p = malloc(sizeof(struct node));" would that accomplish the same without typecasting? – stk Jul 17 '13 at 22:03
  • @stk *`In C, you don't need to cast the return value of malloc. The pointer to void returned by malloc is automagically converted to the correct type. However, if you want your code to compile with a C++ compiler, a cast is needed.`* :~ [dirkgently](http://stackoverflow.com/a/605856/1673391) – Grijesh Chauhan Jul 17 '13 at 22:09
  • Thanks! Why would my book print this unnecessary typecast then? – stk Jul 17 '13 at 22:14
  • Also: is a linked list LIFO or FIFO? – stk Jul 17 '13 at 22:15
  • @stk it stands for some good reasons!! like it not wrong to do typecase if you do correctly, (1) in c++ you need to cast.(3) To avoid warning – Grijesh Chauhan Jul 17 '13 at 22:15
  • `is a linked list LIFO or FIFO ?` **NO**, linked-list is niter LIFO and FIFO. its simply. can be access/insert/delete any where. – Grijesh Chauhan Jul 17 '13 at 22:19
  • Oh! I guess that is the strength of the linked list then! – stk Jul 17 '13 at 22:19
  • Well, can you help me with this then? I have a data file of 50 lines; on each line there is either an integer or a command (INSERT or REMOVE). I am supposed to read the file and follow the commands: INSERT 5, INSERT 10, REMOVE, INSERT 6.. etc.. all of this into a linked list. If the linked list is neither LIFO or FIFO, how am I supposed to know where to insert/remove items into the list? – stk Jul 17 '13 at 22:24
  • @stk **No**, even array is a data-structure in which insertion/deletion can be done from any where (as in linked-list). But array is static-data-structure Whereas linked-lists are dynamic data-structure. – Grijesh Chauhan Jul 17 '13 at 22:24
  • @stk Yes, This is incomplete written assignment Question. If you insert/delete a node in Linked-list, then you should given a position too. For simplicity You can start with insert and remove data from **last**, or **first** – Grijesh Chauhan Jul 17 '13 at 22:28
  • @stk [Here](http://codepad.org/3YaGfYxm) is a code that remove (pop) from first and add (push) value at last (top) :: actually its LIFO implementation using linked-list. – Grijesh Chauhan Jul 17 '13 at 22:32
  • @GrijeshChauhan Will you take a look at http://stackoverflow.com/questions/17711998/linked-list-implementation-in-c-debugging-incorrect-output and see my latest problem? – stk Jul 17 '13 at 23:31
1

malloc returns void*. (struct NODE *) is a cast of this to a pointer to NODE.

This cast is not required in C. Use of it is often considered poor style since it can cause bugs if you forget to include <stdlib.h> for a declaration of malloc.

simonc
  • 41,632
  • 12
  • 85
  • 103
  • Still better than the other - now deleted - answer ("you must cast the pointer") ;) –  Jul 17 '13 at 21:32
  • @simonc If I created a pointer-to-struct-node *p with the statement "struct node *p;" and then allocated memory for it with "p = malloc(sizeof(struct node));" would that accomplish the same without typecasting? – stk Jul 17 '13 at 22:04
  • @stk Yes, it would do the same. What else would it do? –  Jul 17 '13 at 22:09
  • I don't know. I guess I don't understand why my book is typecasting if you all say it's not important. The line of code they printed is: "p = (struct node *)(sizeof(struct node));" which seems to be wrong according to the advice here; why typecast when it's not necessary, if p = malloc(sizeof(struct node)); is an equivalent statement? – stk Jul 17 '13 at 22:13
  • 1
    @stk While the cast is not required in C, C++ compilers do require it. Maybe your book is targeting C++ or at least considering its compilers? – simonc Jul 17 '13 at 22:16