鱼C论坛

 找回密码
 立即注册
查看: 2794|回复: 2

[已解决]求助:为什么我用malloc该函数然后后面一直显示缺少)

[复制链接]
发表于 2020-5-5 22:58:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
# include <malloc.h>
# include <stdio.h>
# include <stdlib.h>
# include <math.h>

# define STACK_INIT_SIZE 100;
# define STACKINCREMENT 10;
typedef char ElemType;
typedef struct{
        ElemType * base;
        ElemType * top;
        int stacksize;
}sqstack;
void intstack(sqstack * s)
{
        s->base=(ElemType *)malloc(STACK_INIT_SIZE * sizeof (ElemType));
}

错误信息:error C2143: syntax error : missing ')' before ';'
最佳答案
2020-5-6 09:03:22
#define 后面一般是不需要添加分号的。
预处理命令是在预编译的时候直接替换的
你在#define中添加了分号,在替换掉 时候就变成了下面的样子
  1. s->base=(ElemType *)malloc(100; * sizeof (ElemType));
复制代码

在malloc中100后面有了分号,系统就认为语句结束了,然后发现缺少半个括号,就报错了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-6 08:38:28 | 显示全部楼层
#define 是预处理命令,后面不用加分号

  1. #include <malloc.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>

  5. #define STACK_INIT_SIZE 100
  6. #define STACKINCREMENT 10
  7. typedef char ElemType;
  8. typedef struct
  9. {
  10.     ElemType *base;
  11.     ElemType *top;
  12.     int stacksize;
  13. } sqstack;
  14. void intstack(sqstack *s)
  15. {
  16.     s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
  17. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-6 09:03:22 | 显示全部楼层    本楼为最佳答案   
#define 后面一般是不需要添加分号的。
预处理命令是在预编译的时候直接替换的
你在#define中添加了分号,在替换掉 时候就变成了下面的样子
  1. s->base=(ElemType *)malloc(100; * sizeof (ElemType));
复制代码

在malloc中100后面有了分号,系统就认为语句结束了,然后发现缺少半个括号,就报错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-8 05:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表