struct node * newNode(int data)
任何人都可以告诉我struct node*
是什么?
node*
代表什么?
提出这个问题背后的意图是:
int main(void){
struct node *node1,*node2;
node1 =(struct node*)malloc(sizeof(*node1));
node2=(struct node*)malloc(sizeof(*node2));
node1->value=7;
node1->left=node2;
node1->right=NULL;
node2->value=9;
node2->left=NULL;
node2->right=NULL;
printf("%d",node1->left->value);
}
这很好用!现在这里为什么结构节点*被铸造?执行struct * node显示错误。
答案 0 :(得分:1)
好像你正在做与二叉树有关的事情。首先,不是这两行: -
node1 =(struct node*)malloc(sizeof(*node1));
node2=(struct node*)malloc(sizeof(*node2));
使用这些(推荐): -
node1=(struct node*)malloc(sizeof(struct node));
node2=(struct node*)malloc(sizeof(struct node));
此外,malloc返回一个void指针,它必须被转换为左侧的数据类型。
node1= malloc( ...);
在左侧(上面的语句),node1的数据类型为struct node
,因此其指针类型为struct node *
。因此,struct node*
用作演员,如下所示。
node1=(struct node*)malloc( ... );
答案 1 :(得分:-1)
struct NODE {
char data[12];
struct NODE* newNode; //reference pointer
};
这里NODE*
只是结构中的引用结构指针,可用于保存结构内声明的变量的地址。
Don't get confused : *node :used as pointer
where as : node* :is used as reference
struct NODE{
char data[12];
char *next;
};
typedef struct DATA Sample;
Sample *hptr = NULL;