看不懂的错误。。
#include <conio.h>
#include<stdio.h>
#include<stdlib.h>
struct snake{
int x;
int y;
struct snake *previous;
struct snake *next;
};
structsnake *head,*pt,*tail;
int main()
{
head=(struct snake*)malloc(sizeof(snake));
head->x=200;
printf("%d\n",head->x);
system("pause");
return 0;
}
为什么我写这段代码调试的时候,监视head->x,编译器VS说运算符要求类和联合?
而我这么写的时候
#include <conio.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
struct snake{
int x;
int y;
struct snake *previous;
struct snake *next;
};
structsnake *head,*pt,*tail;
head=(struct snake*)malloc(sizeof(snake));
head->x=200;
printf("%d\n",head->x);
system("pause");
return 0;
}
监视head->x又显示的是200???谁能说下吗??
[解决办法]
表示两个都显示200,没有错误
[解决办法]
所以,这个是正常的,判断的时候为了防止变成赋值,建议类似的head == NULL写作NULL == head
[解决办法]
head=(struct snake*)malloc(sizeof(snake));
我一直觉得应该改成这样
head=(struct snake*)malloc(sizeof(struct snake));