其中C标准的一部分,允许这种code编译程序吗? [英] Which part of the C standard allows this code to compile?

查看:70
本文介绍了其中C标准的一部分,允许这种code编译程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是bug修复一些code和警告(合法)的编译器,函数 dynscat()未声明 - 别人的可接受编码的想法标准 - 所以我找到了那里的函数定义(很容易)和头宣称它(无;哎呀!)。但我希望找到的结构定义的细节是必要的的extern 的声明 qqparse_val

I was bug-fixing some code and the compiler warned (legitimately) that the function dynscat() was not declared — someone else's idea of an acceptable coding standard — so I tracked down where the function is defined (easy enough) and which header declared it (none; Grrr!). But I was expecting to find the details of the structure definition were necessary for the extern declaration of qqparse_val:

extern struct t_dynstr qqparse_val;

extern void dynscat(struct t_dynstr *s, char *p);
extern void qqcat(char *s);

void qqcat(char *s)
{
    dynscat(&qqparse_val, s);
    if (*s == ',')
        dynscat(&qqparse_val, "$");
}

在原来的code中的 qqcat()函数是静态的; extern声明镇住了这个片段的code编译器警告。在 dynscat()函数声明失踪一共;再次,增加它镇住警告。

The qqcat() function in the original code was static; the extern declaration quells the compiler warning for this snippet of the code. The dynscat() function declaration was missing altogether; again, adding it quells a warning.

使用所示的code片段,很明显,只有可变的地址被使用,因此,在一个级别是有道理的,它并不重要,该结构的细节是未知的。被变量的extern结构t_dynstr * p_parseval; ,你不会看到这个问题;这将是100%的预期。如果code需要访问该结构的内部,然后将所需要的结构的定义。但我一直希望,如果你宣布变量是一个结构(而不​​是一个指向结构),编译器会想知道结构的大小 - 但显然不是

With the code fragment shown, it's clear that only the address of the variable is used, so it makes sense at one level that it does not matter that the details of the structure are not known. Were the variable extern struct t_dynstr *p_parseval;, you'd not be seeing this question; that would be 100% expected. If the code needed to access the internals of the structure, then the structure definition would be needed. But I'd always expected that if you declared that the variable was a structure (rather than a pointer to the structure), the compiler would want to know the size of the structure — but apparently not.

我试图挑起GCC到抱怨,但事实并非如此,甚至GCC 4.7.1:

I've tried provoking GCC into complaining, but it doesn't, even GCC 4.7.1:

gcc-4.7.1 -c -Wall -Wextra -std=c89 -pedantic surprise.c

在code已被编译在AIX,HP-UX,Solaris和Linux的十年,所以它不是GCC-具体的,它被接受。

The code has been compiling on AIX, HP-UX, Solaris, Linux for a decade, so it isn't GCC-specific that it is accepted.

这是C标准允许的(主要是C99或C11,C89,但会做太)?哪部分?还是我刚刚打了所有它移植到了机器的工作原理,但没有正式的标准认可?奇数球的情况下

Is this allowed by the C standard (primarily C99 or C11, but C89 will do too)? Which section? Or have I just hit on an odd-ball case that works on all the machines it's ported to but isn't formally sanctioned by the standard?

推荐答案

看起来像获取对象的地址不完全类型的情况。

Looks like a case of taking the address of an object with incomplete type.

使用指向不完全类型是完全清醒,你每次都这样做,你用一个指针到无效(但从来没有人告诉过你: - )

Using pointers to incomplete types is totally sane and you do it each time you use a pointer-to-void (but nobody ever told you :-)

另一种情况是,如果你声明类似

Another case is if you declare something like

extern char a[];

这并不奇怪,可以分配给的元素,对不对?不过,这是一个不完整的类型和编译器会尽快为您做出这样一个标识符的操作数的sizeof

It is not surprising that you can assign to elements of a, right? Still, it is an incomplete type and compilers will tell you as soon as you make such an identifier the operand of a sizeof.

这篇关于其中C标准的一部分,允许这种code编译程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆