C语言中的常量函数

3
在git的源代码中,我看到了以下函数定义。
const char *typename(unsigned int type)
{
    if (type >= ARRAY_SIZE(object_type_strings))
        return NULL;
    return object_type_strings[type];
}

我认为在这里typename是一个函数,但它似乎是一个const函数,这让我很困惑。
这意味着什么,如何使用这个const函数特性?
源代码链接: https://github.com/git/git/blob/7d722536dd86b5fbd0c0434bfcea5588132ee6ad/object.c#L29

4
这是一个返回const char *的函数。(除了函数的属性static之外,“const”在这里修饰了返回类型。) - M Oehm
1
您解析错误了 - 它只是一个返回 const char * 的函数。 - Paul R
1个回答

11

使用 cdecl

% cdecl
cdecl> explain const char *typename(unsigned int)
declare typename as function (unsigned int) returning pointer to const char

它有时是一个有用的工具,但它受到很大限制,例如它说:

cdecl> explain const char *typename(unsigned int foo);
syntax error

但是在尝试理解函数指针时,它非常有用:

cdecl> declare a as pointer to function (int, double, pointer to const char) 
       returning pointer to const volatile struct foo
const volatile struct foo *(*a)(int , double , const char *)
cdecl> explain const void *(*b[])(int , char (*(*)(char ))(double))
declare b as array of pointer to function (int, pointer to function (char) returning pointer
to function (double) returning char) returning pointer to const void

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接