Reputation: 959
I have an array of strings like such
char *T[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
When I do it like this, however, where each string is an array of unsigned chars
unsigned char *T[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
I get the error "Initializing 'unsigned char *' with an expression of type 'char[5]' converts between pointers to integer types with different sign." I'm guessing that means that some of the representations of "0" and "1" that are getting used are signed, but I'm not sure why/how to deal with that. I'd like to have an array of strings where each string is an array of unsigned chars rather than signed chars. Can someone help with that?
Note: this is for a HW problem, but is not the actual problem, and is just a minor step in one of many possible solutions. However, it would be good if you could help me understand it without giving me an explicit answer. Thanks.
Upvotes: 4
Views: 33458
Reputation: 70949
C strings, one of many ways one could represent a string, consist of arrays of char
terminated by a trailing char
which has the null
value. That's what you get type-wise when you have "0000" in your code.
What you want is to assign "0000" to be an array of unsigned char
terminated by a trailing unsigned char
which has the null value. Considering what you are starting with, you will have to cast, or perhaps represent your initial data in a manner that doesn't require casting.
unsigned char T[][] = { { 0x30, 0x30, 0x30, 0x30, 0x00 },
{ 0x30, 0x30, 0x30, 0x31, 0x00 },
{ 0x30, 0x30, 0x31, 0x30, 0x00 },
{ 0x30, 0x30, 0x31, 0x31, 0x00 },
{ 0x30, 0x31, 0x30, 0x30, 0x00 },
{ 0x30, 0x31, 0x30, 0x31, 0x00 },
{ 0x30, 0x31, 0x31, 0x30, 0x00 },
{ 0x30, 0x31, 0x31, 0x31, 0x00 },
{ 0x31, 0x30, 0x30, 0x30, 0x00 },
{ 0x31, 0x30, 0x30, 0x31, 0x00 },
{ 0x31, 0x30, 0x31, 0x30, 0x00 },
{ 0x31, 0x30, 0x31, 0x31, 0x00 },
{ 0x31, 0x31, 0x30, 0x30, 0x00 },
{ 0x31, 0x31, 0x30, 0x31, 0x00 },
{ 0x31, 0x31, 0x31, 0x30, 0x00 },
{ 0x31, 0x31, 0x31, 0x31, 0x00 }
};
The main problem I see with this approach is that it removes most of the advantage of having a C style string in the first place. With an unsigned char "string", you have none of the standard string libraries at your disposal, so you will have to cast back to signed char string types if you want to use printf
, or any other string oriented function.
Really, you are only using two values for each possible character position "0" and "1". Unless there is a compelling reason to do it in a string, consider an array of boolean values to reduce the chance of a string like "0hello" working it's way into the code, or better yet if you have been introduced to bit fields, use the bits within an unsigned char as bit fields (discarding any concept that you're dealing with strings).
The advantages to the last technique include using less memory and the inability for the value to be other than 0 or 1; however, you will have to write a small collection of routines to translate the packed bits into something human readable.
unsigned char[] = { 0x00, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
0x0F };
void displayChar(unsigned char value) {
switch (value) {
case 0x00: printf("0000"); break;
case 0x01: printf("0001"); break;
case 0x02: printf("0010"); break;
case 0x03: printf("0011"); break;
... and so on ...
Upvotes: 4