简体   繁体   中英

what is the asterisk in front of a variable used for in c++?

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM