算法难题纠错,多谢各位

阿吖呆 2012-11-09 02:22:23
#define MAXSIZE 20 //存储空间初始分配量
typedef struct student
{
int data [MAXSIZE];
int last;
}list;

void init(list *l)
{
l->last = 0;
}

void insert(list *l,int i,int x)
{
int j;

if(i<1||i>l->last+1)
printf("位置错");
else
if(l->last==MAXSIZE-1)
printf("表满");
else
for(i=0; i < 10; i++)
{
L.data[i] = rand()%100;
L.length++;
}
return L;
}

int length(list l)
{
return(l.last);
}

void main()
{
void init(list *l);
int length(list l);
void insert(list *l,int i,int x);

int q;
for(q=0;q<length;q++)
printf("%d",l.data[q]);
}


/*
--------------------Configuration: 表1 - Win32 Debug--------------------
Compiling...
表1.c
E:\我的程序\表1.c(18) : warning C4013: 'printf' undefined; assuming extern returning int
E:\我的程序\表1.c(25) : error C2065: 'L' : undeclared identifier
E:\我的程序\表1.c(25) : error C2224: left of '.data' must have struct/union type
E:\我的程序\表1.c(25) : warning C4013: 'rand' undefined; assuming extern returning int
E:\我的程序\表1.c(26) : error C2224: left of '.length' must have struct/union type
E:\我的程序\表1.c(28) : warning C4098: 'insert' : 'void' function returning a value
E:\我的程序\表1.c(43) : warning C4047: '<' : 'int ' differs in levels of indirection from 'int (__cdecl *)(struct student )'
E:\我的程序\表1.c(44) : error C2065: 'l' : undeclared identifier
E:\我的程序\表1.c(44) : error C2224: left of '.data' must have struct/union type
Error executing cl.exe.

表1.obj - 5 error(s), 4 warning(s)
*/
...全文
220 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
欧阳春晖 2012-11-09
  • 打赏
  • 举报
回复
void main() { void init(list *l); int length(list l); void insert(list *l,int i,int x); int q; for(q=0;q<l->last;q++) printf("%d",l->data[q]); } 你仔细看看,这一段,for循环的l以及printf的l是什么变量, 你在看看其他的函数定义,l被定义为局部参数,main函数有没有l的局部定义,为什么会有l? void init(list *l); int length(list l); void insert(list *l,int i,int x); 这三个是函数声明,里的l是作为形参,又不是局部变量的定义,回去看看C/C++语法书,有关函数的、变量作用域的好好复习一下,你的程序全是语法错误,你在吧我说的错误改掉就可以了,遇到问题先自己想想,不要动不动就问我们,好吗?
Kaile 2012-11-09
  • 打赏
  • 举报
回复
编译错误还要靠自己一点点改
daiyier 2012-11-09
  • 打赏
  • 举报
回复
错误很多,你的main函数里面居然含有这样的形式,在调用函数的时候是不要函数的返回类型的。 void init(list *l); int length(list l); void insert(list *l,int i,int x); 这三行错了,你后面就直接调用l,你都没定义你就去用人家,那是不行的。
yinlincheng 2012-11-09
  • 打赏
  • 举报
回复
#include<stdio.h> #include<stdlib.h> #include<time.h> #define MAXSIZE 20 //存储空间初始分配量 typedef struct student { int data[MAXSIZE]; int length; }list; void init(list *L) { L->length = 0; int i; srand( (unsigned)time( NULL ) ); for(i=0; i < 10; i++) { L->data[i] = rand()%100; L->length++; } } void insert(list *L,int i,int num) { int j; if(i<1||i>L->length+1) printf("位置错"); else if(L->length==MAXSIZE-1) printf("表满"); for(j=L->length-1; j>i; j--) { L->data[j+1]=L->data[j]; } L->data[j]=num; L->length++; } int length(list l) { return(l.length); } int main() { int q; list l; init(&l); printf("after init,before insert\n"); for(q=0;q<length(l);q++) printf("%d ",l.data[q]); printf("\n"); insert(&l,2,10); printf("after insert\n"); for(q=0;q<length(l);q++) printf("%d ",l.data[q]); return 0; }
VermillionTear 2012-11-09
  • 打赏
  • 举报
回复
其实错误提示信息已经写得很明白了。 第18行(警告)使用printf函数时没有引用相关的头文件。 #include <stdio.h> 第41行(错误)标识符l没有定义。 在main函数里定义l,参照之后对l的使用,应该定义为list *l; 接下来41和42行的错误是由于没有定义l所产生的连带错误。 楼主试试吧。
阿吖呆 2012-11-09
  • 打赏
  • 举报
回复
我改了很多,还有一点点想不通,还有朋友看见的请指教 #define MAXSIZE 20 //存储空间初始分配量 typedef struct student { int data [MAXSIZE]; int last; }list; void init(list *l) { l->last = 0; } void insert(list *l,int i,int x) { int j; if(i<1||i>l->last+1) printf("位置错"); else if(l->last==MAXSIZE-1) printf("表满"); else for(j=0;j;j--) l->data[j+1] = l->data[j]; l->data[i] = x; l->last++; } int length(list l) { return(l.last); } void main() { void init(list *l); int length(list l); void insert(list *l,int i,int x); int q; for(q=0;q<l->last;q++) printf("%d",l->data[q]); } /* --------------------Configuration: 表1 - Win32 Debug-------------------- Compiling... 表1.c E:\我的程序\表1.c(18) : warning C4013: 'printf' undefined; assuming extern returning int E:\我的程序\表1.c(41) : error C2065: 'l' : undeclared identifier E:\我的程序\表1.c(41) : error C2223: left of '->last' must point to struct/union E:\我的程序\表1.c(42) : error C2223: left of '->data' must point to struct/union Error executing cl.exe. 表1.obj - 3 error(s), 1 warning(s) */
bookc-man 2012-11-09
  • 打赏
  • 举报
回复
全是语法错误
newtee 2012-11-09
  • 打赏
  • 举报
回复
基本语法错误 自己调试

70,037

社区成员

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

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