1

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?

jwpfox
  • 5,124
  • 11
  • 45
  • 42
  • 6
    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 Sep 23 '17 at 14:33
  • 1
    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()`?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Jonathan Leffler Sep 23 '17 at 16:27
  • 1
    Instead `temp= malloc(sizeof *temp);` Easy - is it not? – chux - Reinstate Monica Sep 24 '17 at 02:49

5 Answers5

1

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.

Bizzu
  • 449
  • 3
  • 17
1

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);
0decimal0
  • 3,884
  • 2
  • 24
  • 39
  • But the question was why are we meantioning the type of the pointer in the malloc function when we have already declared it as type 'struct node*' ? – Aditya Vinod Kumar Sep 28 '17 at 15:43
  • And the answer is still the same , its bad habit . No need to cast the result of malloc. Whoever wrote the code thought that there was a need for that cast which is simply illogical. – 0decimal0 Sep 28 '17 at 20:03
0

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.

babon
  • 3,615
  • 2
  • 20
  • 20
  • Normally the `append` function would allocate a new node in `next` and have the new node point in `prev` to the current (last) node. No storage would be allocated for `prev`. – Paul Ogilvie Sep 23 '17 at 17:26
  • @PaulOgilvie Ahh right. Maybe I was not paying attention when I wrote that. Edited. – babon Sep 24 '17 at 16:05
  • But the question was why are we meantioning the type of the pointer in the malloc function when we have already declared it as type 'struct node*' ? – Aditya Vinod Kumar Sep 28 '17 at 15:43
  • @AdityaVinodKumar As I have mentioned in my answer, the cast is optional and often discouraged. Please read the page I have linked. Click on "often frowned upon" in my answer. – babon Sep 29 '17 at 05:26
-1

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));
}
Aditi Rawat
  • 784
  • 1
  • 12
  • 15
-1

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

Vivek Singh
  • 114
  • 1
  • 8
  • Sorry, you are wrong. Your reference is about integer promotions and conversion of numerical types, not about pointers. @0decimal0's answer is correct. See that reference. – Paul Ogilvie Sep 23 '17 at 17:24
  • I was making a point that return type of malloc is not struct node* type and hence it should be typecasted to the pointer type of struct node – Vivek Singh Sep 23 '17 at 18:36
  • "`malloc` _will return_ `void*` _and when you assign to your_ `struct somthing*` _it will convert automaticlly._" (See other answers.) – Paul Ogilvie Sep 23 '17 at 23:23