2

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

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
rIshab1988
  • 125
  • 1
  • 4
  • 16

7 Answers7

3

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.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • 3
    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
  • @LuchianGrigore: Nothing promises that the sizes of pointer types are all equal. On a hypothetical system where `sizeof (void*) > sizeof (int*)` because `int*` doesn't need so many bits due to alignment requirements, the underlying value most certainly can be different after casting. As another example, there is no guarantee that the null pointer is the same for different pointer types. – jamesdlin Jan 30 '12 at 22:54
1

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

asaelr
  • 5,438
  • 1
  • 16
  • 22
1

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

ouah
  • 142,963
  • 15
  • 272
  • 331
0

Type casting to character pointer.

shadyabhi
  • 16,675
  • 26
  • 80
  • 131
0

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

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.

Christoph
  • 164,997
  • 36
  • 182
  • 240
-2

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;