【error: dereferencing pointer to incomplete type】学unix网络编程,有疑问~~

Redshadows 2011-08-20 07:00:00
#include        <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <mqueue.h>
#include <sys/msg.h>
#include <sys/ipc.h>

#define SVMSG_MODE 420

void
err_quit(const char *s)
{
printf("%s\n", s);
exit(1);
}

int
main(int argc, char **argv)
{
int mqid;
size_t len;
long type;
struct msgbuf *ptr;

if (argc != 4)
err_quit("usage: msgsnd <pathname> <#bytes> <type>");
len = atoi(argv[2]);
type = atoi(argv[3]);

mqid = msgget(ftok(argv[1], 0), 288);

ptr = calloc(sizeof(long) + len, sizeof(char));
ptr->mtype = type;

msgsnd(mqid, ptr, len, 0);

exit(0);
}

编译后显示:
[red@localhost svmsg]$ gcc -o msgsnd msgsnd_cp.c
msgsnd_cp.c: In function ‘main’:
msgsnd_cp.c:19: error: dereferencing pointer to incomplete type

第19行为:ptr->mtype = type

在网上搜索,似乎是联合体/结构体指针的问题,不值如何解决~~

求指点,谢谢~~
...全文
571 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
光明小将 2012-08-29
  • 打赏
  • 举报
回复
我也遇到这个问题,grep -R /usr/include,在这个/usr/include/sys/msg.h:struct msgbuf文件找到,struct msgbuf已经定义了,

自己代码已经把<sys/msg.h>包进来了,怎么还需要自己定义呢?
mymtom 2011-08-21
  • 打赏
  • 举报
回复
这个msgbuf结构需要用户定义,名字也是任意的,只要
至少包括以下的成员即可
long mtype;
char mtext[1];
沭水河畔 2011-08-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 redshadows 的回复:]

谢谢,的确是加了个定义就好
不过很奇怪,struct msgbuf不是应该有定义的吗?不然为什么先前编译时没显示结构体未定义???
引用 1 楼 lishanchao 的回复:

StackOverflow是一个好网站,可以参考这篇。
不知楼主用的是不是Cygwin,我在Cygwin上也无法编译通过,从提示看,就是找不到msgbuf结构体的定义,所以,在合适位置加入如下定义即可:
s……
[/Quote]
楼主可以看一下哪个头文件包含了msgbuf的定义, grep -R msgbuf /usr/include ,看看这个头文件是否被include进来。
struct msgbuf *ptr; 只是声明一个指针,指针的长度和类型都是确定的,所以此处不会有结构体未定义的提示。如果是声明一个变量(struct msgbuf mb1)才会报结构体未定义。

#include <stdio.h>
#include <alloca.h>

struct student
{
char name[20];
int id;
};

#ifdef TEACHER
struct teacher
{
char name[20];
int id;
};
#endif

int main(void)
{
struct student stuA={"Li Lei",1};
struct teacher *techpa=alloca(sizeof(char)*20+sizeof(int));
struct teacher Kong={"Kong qiu",1};

strcpy(techpa->name,"Mrs. Gao");
techpa->id=2;
return 0;
}
Redshadows 2011-08-21
  • 打赏
  • 举报
回复
谢谢,的确是加了个定义就好
不过很奇怪,struct msgbuf不是应该有定义的吗?不然为什么先前编译时没显示结构体未定义???
[Quote=引用 1 楼 lishanchao 的回复:]

StackOverflow是一个好网站,可以参考这篇。
不知楼主用的是不是Cygwin,我在Cygwin上也无法编译通过,从提示看,就是找不到msgbuf结构体的定义,所以,在合适位置加入如下定义即可:
struct msgbuf{
long mtype;
char mtext[1];
};
[/Quote]
Redshadows 2011-08-21
  • 打赏
  • 举报
回复
试过了,没有用~~~ thanks all the same
[Quote=引用 2 楼 feiyinzilgd 的回复:]
C/C++ code

ptr = (struct msgbuf *)calloc(sizeof(long) + len, sizeof(char));
ptr->mtype = type;
[/Quote]
谭海燕 2011-08-20
  • 打赏
  • 举报
回复



ptr = (struct msgbuf *)calloc(sizeof(long) + len, sizeof(char));
ptr->mtype = type;

沭水河畔 2011-08-20
  • 打赏
  • 举报
回复
StackOverflow是一个好网站,可以参考这篇
不知楼主用的是不是Cygwin,我在Cygwin上也无法编译通过,从提示看,就是找不到msgbuf结构体的定义,所以,在合适位置加入如下定义即可:
struct msgbuf{
long mtype;
char mtext[1];
};

23,128

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧