我想在MouseFunc中给一个结构体中成员赋值,但是一直报错Segmentation fault (core dumped),用gdb查了一下是这个赋值语句的问题,以下是代码
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
#include<malloc.h>
#include<stdlib.h>
struct PointD
{
int x;
int y;
PointD* next;
};
struct PointU
{
int x;
int y;
PointU* next;
};
struct PointD* D=NULL;
struct PointU* U=NULL;
void MouseClick(int btn,int state ,int x,int y)
{
x=ReCoordinate(x);
y=ReCoordinate(y);
x=x-half;
y=half-y;
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
D->x=x; //报错语句
D->y=y;//报错语句
PointD* p=NULL;
p=(PointD*)malloc(sizeof(PointD));
D->next=p;//报错语句
D=D->next;
pressed=1;
}
else if(btn==GLUT_LEFT_BUTTON && state==GLUT_UP && pressed==1)
{
U->x=x;
U->y=y;
PointU* q=NULL;
q=(PointU*)malloc(sizeof(PointU));
U->next=q;
U=U->next;
pressed=0;
glutPostRedisplay;
}
}
int main(int argc,char** argv)
{
D=(struct PointD*)malloc(sizeof(PointD));
U=(struct PointU*)malloc(sizeof(PointU));
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE|GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,800);
int half=400;
glutCreateWindow("TEST");
glMatrixMode(GL_PROJECTION);
glOrtho(-half,half,-half,half,-half,half);
glutDisplayFunc(&display);
glutMouseFunc(MouseClick);
glutMainLoop();
return 0;
}