0

I got confused in interpreting this line:

a = (char*) b;

while a and b are both declared to be of type char*.

Can anyone explain it to me please?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294

1 Answers1

2

If they are of the same type, there is no need to use casting in the expression and it is the same as

a = b;

Edit: example casting with pointers

If you are using a C++ compiler, you need casting when calling malloc function otherwise you get an error message. This function returns a void* pointer and you need to explicitly cast it to its destination type as implicit conversion from void* has not been inherited from C.

Quentin
  • 62,093
  • 7
  • 131
  • 191
bpinhosilva
  • 692
  • 1
  • 5
  • 15
  • 1
    No, no, no, no, no, [noooo](http://stackoverflow.com/a/605858/3233393). The compiler does not need your help with the result of `malloc`. – Quentin May 04 '15 at 14:53
  • Thanks for the point, but some compiler versions still send a warning when the cast is not present. – bpinhosilva May 04 '15 at 14:56
  • If they *are* C compilers (not C++), then they are badly broken. No spreading of misinformation in favor of a braindead compiler without mention, please... -1 gone ;) – Quentin May 04 '15 at 14:57