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));