为什么编译错误

HQWEIEI 2010-04-20 09:37:37
#include<stdio.h>
#define N 100
struct stack
{
int *base;
int *top;
}stack
main()
{
stack *a=NULL;
a->base=malloc(sizeof(int));
a->top=a->base+N;
}
系统会出现E:\Program Files\Microsoft Visual Studio\MyProjects\463546\1.c(8) : error C2061: syntax error : identifier 'main'
E:\Program Files\Microsoft Visual Studio\MyProjects\463546\1.c(8) : error C2059: syntax error : ';'
E:\Program Files\Microsoft Visual Studio\MyProjects\463546\1.c(8) : error C2059: syntax error : ')'
为什么,怎样改进
...全文
115 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
happysky1840 2010-04-20
  • 打赏
  • 举报
回复
struct stack
{
int *base;
int *top;
};

void main()
{
stack *a=NULL;
a->base=(int*)malloc(sizeof(int));
a->top=a->base+N;
}

记得FREE!!
庄鱼 2010-04-20
  • 打赏
  • 举报
回复
仅需将 stack *a=NULL;改成 stack *a=(stack*)malloc(sizeof(stack));即可。
空指针是没有成员/成员函数的。
icesky_ff 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 huanmie_09 的回复:]
C/C++ code

#include<stdio.h>

#include<stdio.h>
#include<stdlib.h> /*包含必须的头文件*/

#define N 100

typedef struct stack /*添加typedef,定义结构体类型的同义词*/
{
int *base;
int *top;
} stack;……
[/Quote]
看看这个 呵呵
huanmie_09 2010-04-20
  • 打赏
  • 举报
回复

#include<stdio.h>

#include<stdio.h>
#include<stdlib.h> /*包含必须的头文件*/

#define N 100

typedef struct stack /*添加typedef,定义结构体类型的同义词*/
{
int *base;
int *top;
} stack; /*添加分号*/

int main() /*添加返回值类型*/
{
/*stack *a = NULL;*/ /*下面会报空指针异常,要先分配空间*/
stack *a = (stack *)malloc(sizeof(stack));
a->base = (int *)malloc(sizeof(int));
a->top = a->base; /*添加分号*/

free(a->base); /*释放动态分配的内存*/
free(a);
return 0;
}
FoxMessire 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用楼主 hqweiei 的回复:]
#include<stdio.h>
#define N 100
struct stack
{
int *base;
int *top;
}stack

[/Quote]

结构体要用分号结尾。楼主改了试试
wesleyluo 2010-04-20
  • 打赏
  • 举报
回复
想使自己定义的结构有别名需要用 typedef 修饰。
Willand 2010-04-20
  • 打赏
  • 举报
回复
用malloc的时候记得用free释放内存,这种潜在的BUG会影响系统的稳定性,而且还很难很难发现。
cqhcls 2010-04-20
  • 打赏
  • 举报
回复
代码写得不规范啊...
tan870426 2010-04-20
  • 打赏
  • 举报
回复
可以了~
#define N 100
typedef struct stack
{
int *base;
int *top;
}stack;//分号
void main()
{
stack *a = (stack *)malloc(sizeof(stack));//分配内存
a->base=(int *)malloc(sizeof(int));
a->top=a->base+N;
}
brookmill 2010-04-20
  • 打赏
  • 举报
回复
前面加typedef,后面别忘了分号
brookmill 2010-04-20
  • 打赏
  • 举报
回复
typedef struct stack
{
int *base;
int *top;
}stack;

69,369

社区成员

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

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