C中的const函数

鲍勃

在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

安蒂·哈帕拉(Antti Haapala)

使用 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C ++中的const函数

来自分类Dev

C ++函数体中的const变量

来自分类Dev

关于C ++中const成员函数的问题

来自分类Dev

C中const限定函数指针的行为

来自分类Dev

C ++中const成员函数中的静态成员修改

来自分类Dev

C++中函数中Const用法的区别

来自分类Dev

const函数中的MessageBox

来自分类Dev

函数原型中的const

来自分类Dev

C ++ const函数与参考

来自分类Dev

C ++中的函数const本地对象会发生什么?

来自分类Dev

是在构造函数C ++标准中修改const吗?

来自分类Dev

C ++中成员函数的const&,&和&&&说明符

来自分类Dev

在类构造函数中初始化const向量(C ++)

来自分类Dev

将const char *传递给C中的函数

来自分类Dev

将const char *传递给C中的函数

来自分类Dev

重写const函数以修改C ++中的类变量?

来自分类Dev

C ++中的函数const本地对象会发生什么?

来自分类Dev

在类构造函数中初始化const向量(C ++)

来自分类Dev

在C ++中从T转换为const T的函数

来自分类Dev

在const函数中修改非const属性

来自分类Dev

C ++ getter函数:const和非const

来自分类Dev

在C ++ 11中,应返回临时返回const或non const的函数

来自分类Dev

C++ 在非 const 指针函数中返回一个 const 指针

来自分类Dev

函数原型中的const参数

来自分类Dev

haskell中的const函数声明

来自分类Dev

函数原型中的const参数

来自分类Dev

C ++ const函数错误

来自分类Dev

C ++ Const成员函数(入门)

来自分类Dev

从const成员函数中调用非const成员函数指针