奇怪的 C 函数声明 [英] Weird C function declaration

查看:48
本文介绍了奇怪的 C 函数声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 源文件.

int st_insert(table, key, value)
register st_table *table;
register st_data_t key;
st_data_t value;
{
    unsigned int hash_val, bin_pos;
    register st_table_entry *ptr;

    hash_val = do_hash(key, table);
    FIND_ENTRY(table, ptr, hash_val, bin_pos);

    if (ptr == 0) {
        ADD_DIRECT(table, key, value, hash_val, bin_pos);
        return 0;
    } else {
        ptr->record = value;
        return 1;
    }
}

这是什么风格?声明函数是一种晦涩的方法吗?是否有某种原因可以在普通函数声明中使用它?

What is this style? Is it some obscure way to declare functions? Is there some reason one might use this over normal function declarations?

推荐答案

它是使用标识符列表的函数定义的旧语法(但根据当前的 C 标准仍然有效).标识符的类型在标识符列表之后和左大括号之前声明.

It is an old (but still valid according to the current C Standard) syntax of function definitions using an identifier list. The types of the identifies are declared after the identifier list and before the opening brace.

最好使用带有参数类型列表的函数,因为在这种情况下,具有函数原型的编译器可以检查函数调用的正确参数列表.

It is better to use functions with parameter type lists because in this case the compiler having a function prototype can check the correct list of arguments for a function call.

来自 C 标准(6.9.1 函数定义)

From the C Standard (6.9.1 Function definitions)

6 如果声明符包含一个标识符列表,则每个声明申报清单应至少有一名申报人,即声明者应仅声明标识符列表中的标识符,并且标识符列表中的每个标识符都应该被声明.一个声明为 typedef 名称的标识符不应重新声明为范围.申报清单中的申报不得含有寄存器以外的存储类说明符,没有初始化.

6 If the declarator includes an identifier list, each declaration in the declaration list shall have at least one declarator, those declarators shall declare only identifiers from the identifier list, and every identifier in the identifier list shall be declared. An identifier declared as a typedef name shall not be redeclared as a parameter. The declarations in the declaration list shall contain no storage-class specifier other than register and no initializations.

你可以在旧的 C 代码中遇到其他有趣的结构,例如这个

You can meet other funny constructions in old C code as for example this

memset( ( char * )p, value, n );
        ^^^^^^^^^^  

因为类型 void 是后来引入的.

because type void was introduced later.

这篇关于奇怪的 C 函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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