Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - LinkList *s=(LinkList*)malloc(sizeof(LinkList)); What does it mean? Can you translate it into the popular language of C++?
LinkList *s=(LinkList*)malloc(sizeof(LinkList)); What does it mean? Can you translate it into the popular language of C++?

LinkList: A user-defined linked list, which is a structure, such as typedef struct _LinkList { int id; // Student number char name[16]; // Name struct _LinkList * next; // Point down A node} LinkList; malloc(size): A C language library function used to apply for a piece of dynamic memory. The parameter size is the size of the memory to be applied for, and the address of the applied memory is returned. The address is a void * pointer. This function can use C++'s new instead of sizeof(DataType): to obtain the bytes occupied by a certain data type in memory, so malloc(sizeof(LinkList)) is to apply for a memory of sizeof(LinkList), because the returned An address of void * pointer type. We don't know what the address stores and cannot access it. Therefore, you need to add (LinkList*) in front to convert it into a LinkList type pointer, so that the program knows what content it stores and can access it correctly.

In general, LinkList *s=(LinkList*)malloc(sizeof(LinkList)); is to apply for a LinkList * type data s just like you apply for an integer int n. If you change to dynamic application, you can use int * n = (int *)malloc(sizeof(int)); n is a pointer to an integer. In addition, malloc() can be replaced by new in C++: LinkList *s = new LinkList(); If you want to create multiple n at one time The C language version of LinkList can be like this: LinkList *s = (LinkList*)malloc(sizeof(LinkList) * n); The C++ version can be like this: LinkList *s = new LinkList[ n ]; that's all !!!