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

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

问题描述

这里是 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.

您引用的错误消息表明您在代码中的某处尝试初始化 BitChar &(又名 unsigned char (&)[9] 类型的引用)>) 带有一个 const 限定的 BitChar 数组.这是行不通的,因为它违反了常量正确性的基本规则.引用也必须成为 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]).

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

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