c - Linked Lists and Malloc -


When creating a linked list in the following link, I understand everything that's happening besides:

  curr = (item *) malloc (item of size);  

I think you are allocating enough memory for an item size, but what are you doing with (item *)? Does it mean signal or multiplication?

  contains # lt; Stdlib.h & gt; #to & lt include, stdio.h & gt; Struct list_el {int val; Struct list_el * Next; }; Typedef structure list_el item; Zero main () {item * curr, * head; Int i; Head = null; For (i = 1; i & lt; = 10; i ++) {curr = (item *) malloc (sizeof (item)); Curr- & gt; Val = i; Curr- & gt; Next = head; Head = curue; } Curr = head; While curr {printf ("% d \ n", curr-> val); Curr = curr- & gt; next; }}  

This is an indicative artist: it is zero * malloc (a generic pointer) to a item * (an indicator of your specific type). A zero * is an indicator that can indicate anything, but you can not do any useful work on it such as indirection and arithmetic.

He said, the artist, because C language guarantees that once you specify it, then zero * will be converted to the right type. Just

  curr = malloc (sizeof (item));  

The trick will be performed and the low error prone.


Comments