帮我看看错在哪儿了(关于 malloc),谢谢!

laiben 2003-11-03 09:55:20
s.base=(elem *)malloc(STACK_INIT * sizeof(elem));
//其中elem为一自己定义的结构体,STACK_INIT为常量,如100.
VC中提示
error C2143: syntax error : missing ')' before ';'
error C2059: syntax error : ')'
error C2100: illegal indirection
我看不出有什么')'错了,各位帮帮忙,谢谢!
waiting online 。。。
...全文
42 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
laiben 2003-11-03
  • 打赏
  • 举报
回复
哈哈,我知道了,这个问题解决,谢谢!
不过程序的效果还没出来,我继续改正中。。。。
zjxiaoyu 2003-11-03
  • 打赏
  • 举报
回复
#define STACK_INIT 200; <--------去掉分号
#define STACK_INSERT 50; <--------去掉分号

你看看下面的帖子,刚刚有人根你犯一样的错误。呵呵。
顺便说一下,你的编码风格不太好。
vc里面的cpp的头文件都是没有.h的,比如,
#include <iostream> 等等。你装个VisualAssistant可以看到他们的区别。
还有,cpp里面最好别忘了写
using namespace std;
另外,这种
typedef struct {
int ord;
int *seat;
int di;
}elem;
写法不好,(虽然每次帮你少写struct几个字,但代码不好理解)最好写成
struct ELEM{
..
};
用的时候写上, struct ELEM element;
一点小建议。。
laiben 2003-11-03
  • 打赏
  • 举报
回复
.cpp的,用的是vc6
zjxiaoyu 2003-11-03
  • 打赏
  • 举报
回复
看你的文件头,好像是ANSI-C的,可是,
int initstack(sqstack &s) //这可是c++的。
先问一下,你的文件是.c还是.cpp????同时,你是用的什么编译器??
laiben 2003-11-03
  • 打赏
  • 举报
回复
好,有人愿意看,我已经感到很欣慰了,谢谢大家!
这是一个迷宫程序,虽然方法笨了点(还没确定对否),但也算自己的第一个完整程序吧,各位顺便提出点意见。。。
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <malloc.h>


#define STACK_INIT 200;
#define STACK_INSERT 50;

typedef struct {
int ord;/*serial number of path*/
int *seat;
int di;/*1<<2<<3<<4*/
}elem;

int m[6][10]={ //1 is wall ,0 is path,5 is the black path ,8 is the right path
{0,1,1,1,1,1,1,1,1,1},
{0,1,0,0,1,0,0,0,0,1},
{0,1,0,1,1,0,0,1,0,1},
{0,1,1,1,1,1,1,1,0,1},
{0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,0,0}
};

typedef struct {
elem *base;
elem *top;
int stacksize;
}sqstack;

int initstack(sqstack &s){
s.base=(elem *)malloc(STACK_INIT * sizeof(elem));
if(!s.base) return 0;//error
s.top=s.base;
s.stacksize=STACK_INIT;
return 1;//ok
}

int push(sqstack &s,elem e){
if(s.top-s.base>=s.stacksize){
s.base=(elem *)realloc (s.base,(s.stacksize+STACK_INSERT) * sizeof(elem));
if(!s.base) return 0;//error
s.top=s.base;
s.stacksize+=STACK_INSERT;
}
*s.top++=e;
return 1;//ok
}


int pop(sqstack &s,elem e){
if(s.top==s.base) return 0;//false
e=*--s.top;
return 1;//ok
}


int stackempty(sqstack &s)
{ if(s.top==s.base) return 1;//empty
return 0;//not empty
}

void printmaze(int *ma);

int pass(int *pos);

void footprint(int *posi);

int *nextpos(int* posit,int way);

int stackempty(elem *s);

void markprint(char *st);

int mazepath(int maze[],int *start,int *end);


void main()
{int *mstart=&m[0][0], *mend=&m[5][9];//start==m[0][0],end==m[5][9]
cout<<"迷宫如下:"<<endl;
printmaze(&m[0][0]);
if(!mazepath(*m,mstart,mend))
{cout<<"迷宫的出口路径:"<<endl;
printmaze(&m[0][0]);}
else cout<<"The End!"<<endl;
}


void printmaze(int *ma)
{ for(int b=0;b<6;b++)
{for(int a=0;a<10;a++)
printf("%4d",m[b][a]);
cout<<endl;
}
}


int pass(int *pos)
{if(*pos==0) return 0;//false
return 1;//ok
}

void footprint(int *posi)
{ *posi=8;
}


int *nextpos(int *posit,int way)//.........?
{ switch (way)
{ case 1:
posit=posit++;
break;
case 2:
posit=posit+10;
break;
case 3:
posit=posit--;
break;
case 4:
posit=posit-10;
break;
default://error
cout<<"Error"<<endl;
break;
}

return posit;
}


void markprint(int *st)
{ *st=5;}


int mazepath(int maze[],int *start,int *end)
{ sqstack s;
if(!initstack(s)) return 0;
elem e;
int *curpos=start;//curpos=start
int curstep=1;
do{
if(pass(curpos)){//if pass,there is a way
footprint(curpos); //trail
e.ord=curstep;
e.seat=curpos;
e.di=1;//e=(curstep,curpos,1);
if(!push(s,e)) return 0;
if(curpos==end) return 1;//success
curpos=nextpos(curpos,1);
curstep++;
}
else{
if(!stackempty(s)){
if(!pop(s,e)) return 0;
while(e.di==4 && !stackempty(s)){
markprint(e.seat);
if(!pop(s,e)) return 0;
}
if(e.di<4){
e.di++;
if(!push(s,e)) return 0;
curpos=nextpos(e.seat,e.di);
}
}
}//else
}while(!stackempty(s));
return 0;
}//Mazepath
zjxiaoyu 2003-11-03
  • 打赏
  • 举报
回复
hehe,帖全部代码。这种编译错误还能没法解决?
laiben 2003-11-03
  • 打赏
  • 举报
回复
感谢各位的提醒,可是。。。。依然无法通过。。。。
:-(
angelboycn 2003-11-03
  • 打赏
  • 举报
回复
error C2059: syntax error : ')'
楼主试试把输入法切换到标准英文下,然后重新输入一个‘)’看看,
可能别的输入法输入的')'编译器不认~我写程序的时候,智能ABC输入的一切符号(+,-之类的)编译器都不认~
zjxiaoyu 2003-11-03
  • 打赏
  • 举报
回复
hehe,改为
s.base=(struct elem *)malloc(STACK_INIT * sizeof(struct elem));
~~~ ~~~~
试试。
laiben 2003-11-03
  • 打赏
  • 举报
回复
其实是栈的初始化跟进栈操作
int initstack(sqstack &s){
s.base=(elem *)malloc(STACK_INIT * sizeof(elem));//出错,同上
if(!s.base) return 0;//error
s.top=s.base;
s.stacksize=STACK_INIT;
return 1;//ok
}

int push(sqstack &s,elem e){
if(s.top-s.base>=s.stacksize){
s.base=(elem *)realloc (s.base,(s.stacksize+STACK_INSERT) * sizeof(elem));//出错,同上
if(!s.base) return 0;//error
s.top=s.base;
s.stacksize+=STACK_INSERT;
}
*s.top++=e;
return 1;//ok
}
angelboycn 2003-11-03
  • 打赏
  • 举报
回复
贴的这一行。。。没用~~
proware 2003-11-03
  • 打赏
  • 举报
回复
贴代码
Jinhao 2003-11-03
  • 打赏
  • 举报
回复
多贴几行代码出来看看
daizh 2003-11-03
  • 打赏
  • 举报
回复
是不是你这句话前面的语句少个“;”号呀?

69,371

社区成员

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

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