我读的书上说,malloc 的语法是 malloc(sizeof(int)),但在双向链表程序中,我看到了以下代码:
newnode=(struct node *)malloc(sizeof(struct node))
在这里(struct node*)是干什么的?整个代码是在做什么?顺便说一下,程序中结构体的代码如下。
struct node
{
char line[80];
struct node *next,*prev;
};
struct node *start=NULL,*temp,*temp1,*temp2,*newnode;
谢谢你
int
的函数强制转换为struct node*
。这实际上隐藏了错误。在该行中不应该有任何转换。其次,该问题标记为C
,因此 C++ 在这里无关紧要。第三,编译器(或 IDE)中的错误不是使用糟糕代码的理由。 - Wiz