此代码在 ANSI-C 中似乎不正确,但在 C99 中没问题:
struct a { int x; int y; } z;
C99 和 ANSI-C 中的 struct 有什么区别?
编辑:我忘记了“a”,我的错。此代码在 C99 模式下使用 gcc 编译正常,但在已知不支持所有 C99 扩展的 splint 上出现解析错误。
Edit2:这里是夹板的输出:
Splint 3.1.2 --- 19 Dec 2007
build/ecos_install/include/cyg/fileio/fileio.h:151:5:
Parse Error. Attempting to continue.
build/ecos_install/include/cyg/fileio/fileio.h:151:25:
Cannot recover from parse error.
*** Cannot continue.
Edit3:这个文件是eCos fileio.h(这个片段的最后一行是第152行):
typedef CYG_ADDRWORD cyg_dir;
//=============================================================================
// Filesystem table entry
typedef int cyg_fsop_mount ( cyg_fstab_entry *fste, cyg_mtab_entry *mte );
typedef int cyg_fsop_umount ( cyg_mtab_entry *mte );
typedef int cyg_fsop_open ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
int mode, cyg_file *fte );
typedef int cyg_fsop_unlink ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
typedef int cyg_fsop_mkdir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
typedef int cyg_fsop_rmdir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
typedef int cyg_fsop_rename ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,
cyg_dir dir2, const char *name2 );
typedef int cyg_fsop_link ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,
cyg_dir dir2, const char *name2, int type );
typedef int cyg_fsop_opendir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
cyg_file *fte );
typedef int cyg_fsop_chdir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
cyg_dir *dir_out );
typedef int cyg_fsop_stat ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
struct stat *buf);
typedef int cyg_fsop_getinfo ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
int key, void *buf, int len );
typedef int cyg_fsop_setinfo ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
int key, void *buf, int len );
struct cyg_fstab_entry
{
const char *name; // filesystem name
CYG_ADDRWORD data; // private data value
cyg_uint32 syncmode; // synchronization mode
cyg_fsop_mount *mount;
cyg_fsop_umount *umount;
cyg_fsop_open *open;
cyg_fsop_unlink *unlink;
cyg_fsop_mkdir *mkdir;
cyg_fsop_rmdir *rmdir;
cyg_fsop_rename *rename;
cyg_fsop_link *link;
cyg_fsop_opendir *opendir;
cyg_fsop_chdir *chdir;
cyg_fsop_stat *stat;
cyg_fsop_getinfo *getinfo;
cyg_fsop_setinfo *setinfo;
} CYG_HAL_TABLE_TYPE;
最佳答案
struct { int x; int y; } z;
此代码是有效的 C,具有相同的语义,来自 1978 年以来的每个 C 版本,可能更早。它定义了一个名为 z 的变量,它的类型是一个无名结构类型,由两个整数组成。
ofaurax,您从什么错误消息中得出结论认为它不起作用?
(学究式的吹毛求疵:“ANSI C”是指由美国国家标准协会ANSI标准化的C版本。1989年版的ANSI C标准被国际标准组织ISO采用。1999年ISO制定了新的C 标准的版本,后来被 ANSI 采纳。)
编辑:
struct a { int x; int y; } z;
这定义了一个结构类型,称为“struct a”,由两个整数和一个该类型的变量 z 组成。即使在 1978 年版本的 C(“K&R”)中,这仍然是良构的。我不知道什么是拆分,但确切的错误消息仍然可能帮助我们找出问题所在。
关于c - C99 中的结构与 ANSI-C 有何区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/482884/