【问题标题】:What are the variable names that are disallowed in cdecl other than the ones that cannot be used in any program?除了不能在任何程序中使用的变量名之外,还有哪些在 cdecl 中不允许使用的变量名?
【发布时间】:2015-03-25 14:10:46
【问题描述】:

我一直在玩cdecl,我注意到尽管 GCC 可以完美地编译它们,但其中一些名称不允许作为标识符。

例如,如果我写

int ptr;

int 指针;

整数数组;

cdecl 给出“语法错误”,但是当我在程序中使用它时,GCC 编译它们没有任何问题。所以,有些标识符在 cdecl 中是不允许的。

哪些标识符不能在cdecl中使用,但可以在程序中使用(即程序编译)?为什么不允许?

【问题讨论】:

  • 您是否尝试查看其source code 以了解它如何解释此类标识符?
  • @RemyLebeau , 源码太大。它很难阅读和理解。

标签: c identifier variable-names cdecl


【解决方案1】:

pointerarray 在 cdecl 的保留关键字列表中:

char *keywords[] = {
  "function",
  "returning",
  "array",     // <--
  "pointer",   // <--
  "reference",
  "member",
  "const",
  "volatile",
  "noalias",
  "struct",
  "union",
  "enum",
  "class",
  "extern",
  "static",
  "auto",
  "register",
  "short",
  "long",
  "signed",
  "unsigned",
  "char",
  "float",
  "double",
  "void",
  NULL
};

至于ptr,我不知道为什么cdecl认为那是无效的。在 cdecl 中输入以下表达式也会失败:

将ptr声明为int

但这有效:

将 ptr1 声明为 int

很明显它也不喜欢ptr

【讨论】:

  • "returning" 在输入英文文本翻译成代码时是允许的,例如:function returning int 翻译成int f()。因此保留"pointer""array" 是有意义的,因为它们也用于英文文本。但是"ptr"?哦等等,我刚试过declare foo as array 5 of ptr to int,它被翻译成int *foo[5],所以我猜cdecl把"ptr"当作"pointer"的别名。
  • 这是有道理的。请将其添加到您的答案中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多