char array initialization

F

FlorinBr

Hello! I am trying to initialize a lookup table, which consists of
characters. The problem is that I need the characters in hexadecimal.
So, I tried the following:
<code>
#define UNSUPPORTED '\xff'
typedef struct _this_stuff {
char tut[5];
char a;
char mnames[2][6];
} stuff;
static stuff sh[] ={
{ "ura" , 3 , {
{ UNSUPPORTED , '\xa9' , '\xb9' , UNSUPPORTED ,
'\xb9' }
}
},
{ "mimi" , 4 , {
{ UNSUPPORTED , '\xbb' , UNSUPPORTED , UNSUPPORTED ,
'\xcc' }
}
},
{ "" , 5 , {
/*none*/ }
},
};
main() {
printf("%x.\n", sh[0].mnames[0][2]);
}
<\code>

I get the following output: ffffffb9

However, if I use the more frequently-used initializer for chars,
without an escape sequence, I get the right output. that is, when
using the code:
<code>
#define UNSUPPORTED '\xff'
typedef struct _this_stuff {
char tut[5];
char a;
char mnames[2][6];
} stuff;
static stuff sh[] ={
{ "ura" , 3 , {
{'a','l','b','a'},
}
},
{ "mimi" , 4 , {
{'n','e','a','g','r','a'},
}
},
{ "" , 5 , {
/*none*/ }
},
};
main() {
printf("%x.\n", sh[0].mnames[0][2]);
}
<\code>

I get the output:b

Where is it going wrong?
 
O

Old Wolf

Hello! I am trying to initialize a lookup table, which consists of
characters. The problem is that I need the characters in hexadecimal.
So, I tried the following:
<code>
#define UNSUPPORTED '\xff'
typedef struct _this_stuff {
char tut[5];
char a;
char mnames[2][6];} stuff;

static stuff sh[] ={
{ "ura" , 3 , {
{ UNSUPPORTED , '\xa9' , '\xb9' , UNSUPPORTED ,
'\xb9' }
}
},
{ "mimi" , 4 , {
{ UNSUPPORTED , '\xbb' , UNSUPPORTED , UNSUPPORTED ,
'\xcc' }
}
},
{ "" , 5 , {
/*none*/ }
},};

main() {
printf("%x.\n", sh[0].mnames[0][2]);}

<\code>

I get the following output: ffffffb9
Where is it going wrong?

Firstly, you cause undefined behaviour by passing a signed char
as the argument corresponding to '%x' in printf. %x is only to
be used with unsigned int values. One way to fix this would be:

printf("%x.\n", (unsigned int)(unsigned char)sh[0].mnames[0][2]);

Technically you need the first cast, but on most systems it doesn't
matter.

Secondly, you cause implementation-defined behaviour with '\xa9',
'\xff', etc. On your system, 'char' is signed, so its value range
is -128 through to 127. It's not reliable to try and store 169 or 255
in such a variable.

Your simplest solution would be to switch to using 'unsigned char'
as the type of your array, and specify the values as 0xFF, 0xA9,
and so on, instead of '\xFF' etc.
 
F

FlorinBr

Hello! I am trying to initialize a lookup table, which consists of
characters. The problem is that I need the characters in hexadecimal.
So, I tried the following:
<code>
#define UNSUPPORTED '\xff'
typedef struct _this_stuff {
char tut[5];
char a;
char mnames[2][6];} stuff;
static stuff sh[] ={
{ "ura" , 3 , {
{ UNSUPPORTED , '\xa9' , '\xb9' , UNSUPPORTED ,
'\xb9' }
}
},
{ "mimi" , 4 , {
{ UNSUPPORTED , '\xbb' , UNSUPPORTED , UNSUPPORTED ,
'\xcc' }
}
},
{ "" , 5 , {
/*none*/ }
},};
main() {
printf("%x.\n", sh[0].mnames[0][2]);}

I get the following output: ffffffb9
Where is it going wrong?

Firstly, you cause undefined behaviour by passing a signed char
as the argument corresponding to '%x' in printf. %x is only to
be used with unsigned int values. One way to fix this would be:

printf("%x.\n", (unsigned int)(unsigned char)sh[0].mnames[0][2]);

Technically you need the first cast, but on most systems it doesn't
matter.

Secondly, you cause implementation-defined behaviour with '\xa9',
'\xff', etc. On your system, 'char' is signed, so its value range
is -128 through to 127. It's not reliable to try and store 169 or 255
in such a variable.

Your simplest solution would be to switch to using 'unsigned char'
as the type of your array, and specify the values as 0xFF, 0xA9,
and so on, instead of '\xFF' etc.


Thank you, Old Wolf! It worked! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top