如何将char数组定义为常量? [英] How do you define a char array as a constant?

查看:145
本文介绍了如何将char数组定义为常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C/C ++菜鸟在这里.我已经在头文件中定义了它...

C/C++ noob here. I've defined this in a header file...

typedef unsigned char BitChar[9]; // 8 data bytes (chars) and one width byte (char)

extern BitChar BitFont[];

我在cpp文件中有这个文件...

and I have this in a cpp file...

BitChar BitFont[] = {
    B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,2, // 32 - Space
    B10000000,B10000000,B10000000,B10000000,B10000000,B00000000,B10000000,B00000000,1, // 33 - !
    ...
    B00000000,B00000000,B11100000,B11100000,B11100000,B00000000,B00000000,B00000000,3, // 127 - Unknown
};

它可以编译,并且看起来运行良好.但是,由于它永远不会改变,因此我认为应该将其标记为常量.我该如何标记呢?我所期望的,加上'const',会引发编译错误,所以我很困惑.这是错误...

It compiles and seemingly runs just fine. However, since it will never change, I thought it should be marked as a constant. How do I mark it as such? What I expected, adding 'const', throws compile errors so I'm stumped. Here's the error...

error: invalid initialization of reference of type 'unsigned char (&)[9]' from expression of type 'const unsigned char [9]' 

推荐答案

只需添加const.

extern const BitChar BitFont[];
...
const BitChar BitFont[] = {
    B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,2, // 32 - Space
    B10000000,B10000000,B10000000,B10000000,B10000000,B00000000,B10000000,B00000000,1, // 33 - !
    ...
    B00000000,B00000000,B11100000,B11100000,B11100000,B00000000,B00000000,B00000000,3, // 127 - Unknown
};

在C语言中应该可以正常工作(假设您的编译器知道这些B00000000标识符的含义.)

should work perfectly fine in C. (Assuming that your compiler knows what these B00000000 identifiers mean.)

这在C ++中也可以很好地工作. C ++版本中唯一可能出错的是基于const的C ++特定属性.如果定义没有看到声明,那么您还必须在定义中指定显式的extern

This will also work perfectly fine in C++. The only potential for error in the C++ version is based on C++-specific properties of const. If the definition does not see the declaration, then you have to specify the explicit extern in the definition as well

extern const BitChar BitFont[] = {
    B00000000
    ...

因为在C ++中,const对象默认具有内部链接.但是,如果声明中已经包含extern并且定义可以看到该声明,则定义中的extern是可选的.

because in C++ const objects have internal linkage by default. However, if the declaration already contains the extern and the definition can see the declaration, then that extern in the definition is optional.

您引用的错误消息表明,您试图在代码中的某个地方尝试使用const限定的BitChar数组初始化类型为BitChar &(又名unsigned char (&)[9])的引用.这将不起作用,因为它违反了const-correctness的基本规则.引用也必须变为const限定,即必须更改为const BitChar &(又名const unsigned char (&)[9]).

The error message you quoted suggests that somewhere in your code you are trying to initialize a reference of type BitChar & (aka unsigned char (&)[9]) with a const-qualified BitChar array. This will not work, since it violates the basic rules of const-correctness. The reference has to become const-qualified as well, i.e. it has to change to const BitChar & (aka const unsigned char (&)[9]).

这篇关于如何将char数组定义为常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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