求助编译错误:illegal use of this type as an expression

ZhengZhiRen 2008-11-09 04:39:08
实在不知道哪错了,急啊。
帮忙看看,谢了!

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
int i;
struct node *next;
}node;

void fun(node *p,int i)
{
int count=1;
node *q;
q=p->next;
while(q->next!=q)
{
if(count%i==0)
{
p->next=q->next;
printf("%d",q->i);
free(q);
}
else
p=p->next;
q=p->next;
count++;
}
printf("%d",q);
}

void main()
{
int i;
int num;
printf("please input number:\n");
scanf("%d",&num);
node *circle;
node *p;
node *q;
/*初始化循环链表*/
circle=(node*)malloc(sizeof(node));
circle->i=1;
circle->next=circle;
p=circle;
for(i=2;i<=100;i++)
{
q=(node*)malloc(sizeof(node));
q->i=i;
q->next=p->next;
p->next=q;
p=p->next;
}
fun(circle,num);
}



--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.c
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(36) : error C2275: 'node' : illegal use of this type as an expression
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(8) : see declaration of 'node'
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(36) : error C2065: 'circle' : undeclared identifier
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(37) : error C2275: 'node' : illegal use of this type as an expression
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(8) : see declaration of 'node'
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(37) : error C2065: 'p' : undeclared identifier
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(38) : error C2275: 'node' : illegal use of this type as an expression
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(8) : see declaration of 'node'
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(38) : error C2065: 'q' : undeclared identifier
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(40) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct node *'
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(41) : error C2223: left of '->i' must point to struct/union
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(42) : error C2223: left of '->next' must point to struct/union
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(46) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct node *'
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(47) : error C2223: left of '->i' must point to struct/union
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(48) : error C2223: left of '->next' must point to struct/union
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(48) : error C2223: left of '->next' must point to struct/union
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(49) : error C2223: left of '->next' must point to struct/union
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(50) : error C2223: left of '->next' must point to struct/union
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(52) : warning C4047: 'function' : 'struct node *' differs in levels of indirection from 'int '
D:\My Documents\程序\VC6.0\约瑟夫环\main.c(52) : warning C4024: 'fun' : different types for formal and actual parameter 1
Error executing cl.exe.

main.obj - 13 error(s), 4 warning(s)
...全文
2773 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
galilio 2009-04-20
  • 打赏
  • 举报
回复
推荐使用
typedef struct node{
****
}node
ZhengZhiRen 2008-11-09
  • 打赏
  • 举报
回复
另外问下
typedef struct _node
{
int i;
struct _node *next;
}node;
这里重名会出问题吗?我看一些书上就是重名的啊。
ZhengZhiRen 2008-11-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 Chiyer 的回复:]
帮你改好了


int i;
int num;
node *circle;
node *p;
node *q; /*在c语言里要把所有定义放最前面*/

printf("please input number:\n");
scanf("%d",&num);


[/Quote]

谢谢你!
node *circle;
node *p;
node *q;
是我后来加上的,忘了放在scanf后面了!解决了
星羽 2008-11-09
  • 打赏
  • 举报
回复
帮你改好了



#include <stdio.h>
#include <stdlib.h>

/* 这里定义要改成这样,你之前重名了*/
typedef struct _node
{
int i;
struct _node *next;
}node;

void fun(node *p,int i)
{
int count=1;
node *q;
q=p->next;
while(q->next!=q)
{
if(count%i==0)
{
p->next=q->next;
printf("%d",q->i);
free(q);
}
else
p=p->next;
q=p->next;
count++;
}
printf("%d",q);
}

void main()
{
int i;
int num;
node *circle;
node *p;
node *q; /*在c语言里要把所有定义放最前面*/

printf("please input number:\n");
scanf("%d",&num);

/*初始化循环链表*/
circle=(node*)malloc(sizeof(node));
circle->i=1;
circle->next=circle;
p=circle;
for(i=2;i <=100;i++)
{
q=(node*)malloc(sizeof(node));
q->i=i;
q->next=p->next;
p->next=q;
p=p->next;
}
fun(circle,num);
}
帅得不敢出门 2008-11-09
  • 打赏
  • 举报
回复
vc6.0也编译通过
代码没啥问题的
你rebuild一下
或者新建个工程试试
还有工程类型没选错吧
lzr4304061988012 2008-11-09
  • 打赏
  • 举报
回复
LZ,vs2008编译通过,且正常运行
帅得不敢出门 2008-11-09
  • 打赏
  • 举报
回复
你用的什么编译器
除了main返回int外 gcc编译其他代码没有问题啊.
ZhengZhiRen 2008-11-09
  • 打赏
  • 举报
回复
这个代码看得清楚点

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
int i;
struct node *next;
}node;

void fun(node *p,int i)
{
int count=1;
node *q;
q=p->next;
while(q->next!=q)
{
if(count%i==0)
{
p->next=q->next;
printf("%d",q->i);
free(q);
}
else
p=p->next;
q=p->next;
count++;
}
printf("%d",q);
}

void main()
{
int i;
int num;
printf("please input number:\n");
scanf("%d",&num);
node *circle;
node *p;
node *q;
/*初始化循环链表*/
circle=(node*)malloc(sizeof(node));
circle->i=1;
circle->next=circle;
p=circle;
for(i=2;i<=100;i++)
{
q=(node*)malloc(sizeof(node));
q->i=i;
q->next=p->next;
p->next=q;
p=p->next;
}
fun(circle,num);
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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