-4

I am currently learning linked list and my prof sent us a code which is so hard to understand for me. I know that asterisk is used before the varaible to make it as a pointer but this one is infront of a variable.

Here is the code:

#include <iostream>
using namespace std;
struct Node { 
   int data; 
   struct Node *next; 
}; 
struct Node *head = NULL;   
void insert(int new_data) { 
   struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); 
   new_node->data = new_data; 
   new_node->next = head; 
   head = new_node; 
} 
void display() { 
   struct Node* ptr;
   ptr = head;
   while (ptr != NULL) { 
      cout<< ptr->data <<" "; 
      ptr = ptr->next; 
   } 
} 
int main() { 
   insert(3);
   insert(1);
   insert(7);
   insert(2);
   insert(9);
   cout<<"The linked list is: ";
   display(); 
  return 0; 
}

This is the one that I am talking about:

void insert(int new_data) { 
       struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); 
       new_node->data = new_data; 
       new_node->next = head; 
       head = new_node; 
    } 

I dont know what is the purpose of the asterisk in here (struct Node*) malloc(sizeof(struct Node));\

and can someone tell me what is the purpose of malloc here malloc(sizeof(struct Node))

JaMiT
  • 9,693
  • 2
  • 12
  • 26
newbie
  • 1
  • `struct Node *head = NULL;` In `c++` you don't need the `struct` part. `Node *head = NULL;` is sufficient. – drescherjm Aug 14 '20 at 18:11
  • 4
    Unfortunately, you are actually looking at C code that is being compiled by a C++ compiler... – jxh Aug 14 '20 at 18:12
  • C++ is a context sensitive language. `*` means different things in different contexts. – Jesper Juhl Aug 14 '20 at 18:12
  • I agree except for the `cout` this is `c` code. – drescherjm Aug 14 '20 at 18:13
  • @drescherjm and that `NULL` really ought to be `nullptr`. – Jesper Juhl Aug 14 '20 at 18:13
  • 1
    I would also complain about the malloc – drescherjm Aug 14 '20 at 18:14
  • Does this answer your question? [I don't understand pointers](https://stackoverflow.com/questions/43706050/i-dont-understand-pointers) – user1937198 Aug 14 '20 at 18:15
  • My guess is you are confused with this line: `struct Node *head = NULL;` is the same as `struct Node* head = NULL;` or `struct Node * head = NULL;` It does not matter if there is a space before or after the `*` – drescherjm Aug 14 '20 at 18:15
  • 1
    *"I know that asterisk is used before the varaible to [...] but this one is infront of a variable."* -- huh? Being in front of a variable does not qualify as being before a variable? – JaMiT Aug 14 '20 at 18:17
  • 4
    Welcome to Stack Overflow! While this site does encourage new programmers to use this site to learn, this is a basic syntax question that should have been covered in your reference book. Because of that, the question could be considered to have lacked sufficient research before being asked. If you did look it up, but found the explanations inadequate, you can post the research you did, and explain why it did not answer your question. – jxh Aug 14 '20 at 18:17
  • Related: https://stackoverflow.com/q/184537/315052 – jxh Aug 14 '20 at 18:30
  • Related: https://stackoverflow.com/q/28002/315052 – jxh Aug 14 '20 at 18:32
  • 1
    Side note: In C++ the entire `insert` function can be reduced to `head = new Node{new_data, head};`. Much less fuss, no? – user4581301 Aug 14 '20 at 18:40
  • @user1937198 - The OP claims to understand syntax to create a pointer variable. The OP seems to be asking about the cast syntax and about what `malloc` does. – jxh Aug 14 '20 at 18:56
  • Related: https://stackoverflow.com/q/8800482/315052 – jxh Aug 14 '20 at 18:57

3 Answers3

0

This asterisk is after the type name. It means "pointer to that type", in this case, pointer to struct Node.

Let's see this line of code as a whole: struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

Here, a variable new_node is declared, it has the type "pointer to struct Node". The function malloc allocates a piece of memory and returns a pointer to it. However, it doesn't know the type of the pointer, so it returns it as void* (pointer to something unknown).

That's why you need to cast it to the right type before assignment. (struct Node*) is a cast expression, it changes the type of the pointer to the "pointer to struct Node".

To sum up, this line of code allocates a piece of memory to store struct Node in it and saves its address in new_node variable.

But yes, as others have noted, it's not C++ code, it's C code.

Alexandr Zarubkin
  • 736
  • 1
  • 9
  • 23
0

In this case the "(struct Node*)" part means it is a struct pointer, in this case the Node struct, it means that it is pointing to the struct, in this case since it is a linked list, you'll have a bunch of this Node Structs all pointing to the "next" one until it reaches the head of the list which is

"struct Node *head = NULL; " then in this function

Then you use this function

void insert(int new_data) { 
       struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); 
       new_node->data = new_data; 
       new_node->next = head; 
       head = new_node; 
    }

What it does is create a new Node and point it to the previous created Node, it would look something like this:

head<-Node1<-Node2<-Node3...

and can someone tell me what is the purpose of malloc here malloc(sizeof(struct Node))

Malloc here just tells the compiler to allocate the amount of memory the size of the struct called Node so that it doesn't use anymore or anyless then needed.

Also I think this is C not C++

https://www.programiz.com/cpp-programming/library-function/cstdlib/malloc

Jump
  • 3
  • 2
0

In the above code you posted, you are declaring data type of "struct Node" . If you want to declare integer pointer you will do as below :

int *iPtr;

Similarly, here you are declaring pointer to data type struct Node, so it will be

struct Node *new_node;

I guess you got confused between data type and variable . Here "struct Node" is an user defined data type and "next", "head" and "new_node" are variable names.

And regarding malloc, malloc() is a function that will allocate memory in heap for the size of the data type you want and here you want to allocate memory for sizeof(struct Node) which is a size of one Linked list node.

And the malloc() function allocates sizeof(struct Node) bytes and returns a void pointer to the allocated memory and since malloc returns a void pointer (generic pointer) you have to typecast it to which ever data type you are allocating for.

Again I will give you simple example :

int *iPtr = (int *)malloc(sizeof(int));

Similarly, here for data type Struct Node it is done as below:

struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); 
Nandish
  • 882
  • 5
  • 12
  • I am confused with the parenthesis used in here (struct Node*) malloc(sizeof(struct Node));. Can I ask why cant it be like this: struct Node *malloc(sizeof(struct Node)); – newbie Aug 15 '20 at 03:56
  • As I mentioned in my answer you have to typecast void pointer to struct Node pointer and typecast is done by using parenthesis like (type_name) expression. It is basic syntax – Nandish Aug 15 '20 at 06:41