I know char *x
means a pointer to char, but I\'m confused about what (char*) x
means.
declaration of a variable called "x" of type char:
char x;
declaration of a variable called "x" which is a pointer to a char:
char *x;
cast of something that is not a char to type char:
int x = 10;
char y = (char)x;
()
is the cast operator.
(char *) x
means "apply the cast operator to operand x".
The cast operator converts the value of the operand to the type between ()
.
Type casting to character pointer.
Actually, char *x
is a declaration. x
is pointer to char.
If you already have a variable, such as x
, you can cast it to a different type. In your case, (char*) x
is redundant, since x
already is a pointer to char. But you can cast it to int *
or something else. Although, note, it's not safe, since an int is larger than a char.
It means casting x to a pointer to char (or to a general pointer).
It's a cast. You are instructing the compiler to treat x
as if it were a char *
, regardless of its real type. Casts should only be used if you really know what you are doing.
For some built-in types, the compiler may perform a meaningful conversion, e.g. converting a double
to an int
by rounding, but for other types you may not get what you expect.