1
votes

In creating a linked list we make a node structure and it consists of both data and a pointer to the next node. Later when we make a function to append elements onto the linked list, we make a temporary node to store the inputted data.

Let’s consider the following program-

#include<stdio.h>
struct node
{
  int data;
  struct node* link;
}
struct node* root=NULL;
void main(append)
{
  struct node* temp;
  temp= (struct node*)malloc(sizeof(struct node))
  .....
}

My first question set:

In line 11, why do we need to mention (struct node*) before the malloc function?

What is the significance of that?

My second question set:

If we are making a doubly linked list which would have a node structure consisting of 2 pointers (for next and previous node), would we also initialize a pointer (for traversing the list) of the struct node type?

Is there a different way to initialize the pointer in that case?

5
Who told you you need that? It is even strongly discouraged. In general, never use unnecessary casts, they will eventually drop on your head one day. If you got that from your C book, get a better one. If from some obscure youtube vvideo, blog or online-tutorial: Get a good C book. - too honest for this site
Note that if you use a C++ compiler to compile C code, that cast would be necessary. It isn't necessary in C that's only compiled by C compilers. Also note Do I cast the result of malloc()? - Jonathan Leffler
Instead temp= malloc(sizeof *temp); Easy - is it not? - chux - Reinstate Monica

5 Answers

1
votes

The significance is to make bugs in your program,

The malloc will return void* and when you assign to your struct somthing* it will convert automaticlly.

1
votes

You simply don't cast the result of malloc as it returns void* . There is one fine explanation here

A better solution might be :

struct node *temp;
temp = malloc(sizeof *temp);
0
votes

why do we need to mention '(struct node*)' before the malloc function, what is the significance of that?

By writing (struct node*) before the malloc function, you are type-casting the return value to the specified type. The cast here is optional and often frowned upon.

if we are making a doubly linked list which would have a node structure consisting of 2 pointers(...

When making a doubly linked list, you should declare something like:

struct node {
    int data;
    struct node *next;
    struct node *previous;
};

You can allocate space for a node by using the malloc function. The next and previous pointers are again pointers to struct nodes. Call malloc again to allocate space for the next element. For the first node, the previous should be NULL and for the last node, next should be NULL. Here is one implementation.

-1
votes

This is the because the return type of malloc is void*. (struct node*) is a cast using which you tell the compiler that you want to treat the value returned by malloc as a pointer to struct node.

For double linked list you can use,

struct node
      {
       int data;
       struct node *next,*prev;
       };

int main()
{
   struct node *new_node=(struct node *)malloc(sizeof(node));
}
-1
votes

malloc returns the void pointer(can be checked and verified at the header ), and so the type casting is necessary while assigning it to the other type of variable.

Request you to read at the link https://www.tutorialspoint.com/cprogramming/c_type_casting.htm