You don't need a cast here.
struct abc {
char k;
};
int main()
{
struct abc data[] = {.k= 'TI'};
}
Your object data
is an array of struct abc
. The initializer is for a single object of type struct abc
.
If you want data
to be a 1-element array, you can do this:
struct abc data[] = {{.k= 'TI'}};
or, if you want to be more explicit:
struct abc data[] = {[0] = {.k = 'TI'}};
That's valid code, but it's likely to trigger a warning. 'TI'
is a multi-character constant, an odd feature of C that in my experience is used by accident more often than it's used deliberately. Its value is implementation-defined, and it's of type int
.
Using gcc on my system, its value is 21577
, or 0x5449
, which happens to be ('T' << 8) + 'I'
. Since data[0].k
is a single byte, it can't hold that value. There's an implicit conversion from int
to char
that determines the value that will be stored (in this case, on my system, 73
, which happens to be 'I'
).
A cast (not a "type cast") converts a value from one type to another. It doesn't change the type of an object. k
is of type char
, and that's not going to change unless you modify its declaration. Maybe you want to have struct abc { int k; };
?
I can't help more without knowing what you're trying to do. Why are you using a multi-character constant? Why is k
of type char
?