struct FDPARAM
{
int fd0;
int fd1;
};
int main()
{
inf fd0,fd1;
FDPARAM *pFdParam;
pFdParam->fd0 = fd0;
pFdPara->fd1 = fd1;
return 0;
}
以上代码在windows下是没有问题的,但是在linux环境下会出现两个问题:
1.undeclared first use in this function(编译时)
pFdParam在声明时要加类型 struct FDPARAM *pFdParam;
2.段错误(运行时)
段错误的出现多半是因为指针试图引用或修改不存在的物理地址,在linux下,结构体指针实例化时一定要记得初始化
正确的使用方法是:
struct FDPARAM *pFdParam ; // NOTICE: in linux need "struct"
pFdParam = (struct FDPARAM*)malloc(sizeof(struct FDPARAM));//NOTICE:in linux struct need initialization after instantiation
pFdParam->fd0 = fd0;
pFdParam->fd1 = fd1;