Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you define a char array as a constant?

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[];

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
};

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]' 
like image 293
Mark A. Donohoe Avatar asked Aug 15 '14 03:08

Mark A. Donohoe


People also ask

How do you define a constant array?

In C#, use readonly to declare a const array. public static readonly string[] a = { "Car", "Motorbike", "Cab" }; In readonly, you can set the value at runtime as well unlike const.

How do you define a char array?

Declaration and Initialization “array_name = new char[array_length]” is the syntax to be followed. Anyways we can do both declaration and initialization in a single by using “char array_name[] = new char[array_length]” syntax. The length of the array should be declared at the time of initialization in a char array.

How do you define a constant character?

const char* is a pointer to a constant char, meaning the char in question can't be modified. char* const is a constant pointer to a char, meaning the char can be modified, but the pointer can not (e.g. you can't make it point somewhere else).

How do you declare an array constant in C++?

In C++, the most common way to define a constant array should certainly be to, erm, define a constant array: const int my_array[] = {5, 6, 7, 8};


1 Answers

Just add const. This

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
};

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

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
    ...

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.

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]).

like image 188
AnT Avatar answered Sep 20 '22 15:09

AnT