Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Hidden byte in unsigned char[]

unsigned char blind[32] = "and my kingdom too for a blinder";

This gives the error:

a value of type "const char [33]" cannot be used to initialize an entity of type "unsigned char [32]"C/C++(144)

I do not understand why this gives an error.

I’m trying to use a C library in a C++ project, and wanted to send the type the C library asked for.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem is that "and my kingdom too for a blinder" is a string literal of type const char[33] and not const char[32]. The last character is the null character '\0'. This means that the string literal "and my kingdom too for a blinder" is equivalent to:

{'a', 'n', 'd', ' ', 'm', 'y', ' ', 'k','i','n','g','d','o','m',' ','t','o','o',' ','f','o', 'r', ' ', 'a', ' ', 'b', 'l', 'i', 'n', 'd', 'e', 'r', '\0'}

Note in the above const char equivalent array there are 33 characters including the null character '\0'.

To solve this just change the size of the destination array as shown below:

//---------vv------------------------------------------->changed to 33
char blind[33] = "and my kingdom too for a blinder";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading