Take the 2-minute tour ×

I know char *x means a pointer to char, but I'm confused about what (char*) x means.

share|improve this question
add comment

7 Answers

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.

share|improve this answer
It's not just treating. the value of (char*)x may be different from the value of x (even if x is pointer) –  asaelr Jan 30 '12 at 21:49
Casting pointers doesn't modify the value. So it doesn't convert anything. Please verify your answer and change it accordingly, you're giving wrong information here. –  Luchian Grigore Jan 30 '12 at 21:55
If I'm not mistaken, nothing in the standard promise that. –  asaelr Jan 30 '12 at 22:02
@Luchian I said for SOME built-in types, the compiler MAY perform a meaningful conversion. This is absolutely correct. –  Graham Borland Jan 30 '12 at 22:06
@LuchianGrigore, @GrahamBorland: a cast is conceptionally always a conversion - it just so happens that it's a noop in case of compatible representations; calling this 'to treat a value as if it were a different type' is misleading - that's what reinterpret_cast in C++ is for; in C, you can achieve the same thing via type-punning thorugh unions –  Christoph Jan 30 '12 at 22:13
show 1 more comment

It means casting x to a pointer to char (or to a general pointer).

share|improve this answer
add comment

() 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 ().

share|improve this answer
add comment

Type casting to character pointer.

share|improve this answer
add comment

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.

share|improve this answer
add comment

The expression (char *)x is a type cast which returns the value of x converted to type char *.

The results of a cast can be very different depending on the operand and target types: For example, you can use casts to round floating-point values to integer precision, get rid of qualifiers like const, convert a pointer to integer for advanced address calculations like alignment checks and many other things.

However, not all possible casts result in legal values - eg, casting pointers can violate aliasing and alignment rules.

Converting a (valid) non-function pointer to char * is always legal and useful when doing byte-based pointer arithmetics.

share|improve this answer
add comment

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;
share|improve this answer
You addressed three questions he didn't ask but not the one he did. –  Carey Gregory Jan 30 '12 at 21:52
add comment

Your Answer


