0

I am learning linked lists and when allocating memory for structs, I've seen 2 ways to do it. After testing, both work and I wonder what the first option really does. Here is the code:

typedef struct sllist  
{
    int data;
    struct sllist *next;
} node;     


node* create(int data, node* next)
{
    node* new_node = (node*)malloc(sizeof(node));    
    if(new_node == NULL )   
    {
        printf("Error creating a new node\n");
        exit(0);
    }
    new_node->data = data;  
    new_node->next = next;  

    return new_node;
}

On this line:

node* new_node = (node*)malloc(sizeof(node)); 

What is the use of (node*) just preceding malloc? Wouldn't this be the same thing?

node* new_node = malloc(sizeof(node)); 
PYB
  • 503
  • 6
  • 20
  • PS--Regarding your last question..yes, in practice, it's definitely going to be the same result. – zzxyz Aug 23 '18 at 22:39
  • I've deleted my first comment as the academic differences between the two styles are better covered in the original question's answers. – zzxyz Aug 24 '18 at 00:23

0 Answers0