2

Code:

void *buff; 
char *r_buff = (char *)buff;

I can't understand the type casting of buff. Please help.

Thank you.

Chicko Bueno
  • 337
  • 2
  • 11
  • 25

4 Answers4

10

buff is a pointer to some memory, where the type of its content is unspecified (hence the void).

The second line tells that r_buff shall point to the same memory location, and the contents shall be interpreted as char(s).

phimuemue
  • 34,669
  • 9
  • 84
  • 115
  • Thank you! Now I understand. But what is the need of `(char *)`? Is it used to convert anything pointed by buff will then converted into `char`? – Chicko Bueno Mar 14 '11 at 10:49
  • 1
    @Chicko Bueno: It is to indicate that the memory pointed by `r_buf` is of type `char`. So when you do `*buf` you get a `char`. Also, when you do `r_buf++` r_buf will be incremented by `sizeof(char)` to point to the next char in memory. – Naveen Mar 14 '11 at 10:53
  • "convert" and "cast" are not the same thing. "convert" can mean to take and integer and create an string whose content is an integer, eg. 123 -> "123". "cast" means to treat some content that is in memory as specific type. for instance you store chars in memory but you want to treat them as unsigned chars. you don't want to modify them (read convert) them, you just need to operate on them as they where a different type. that is casting – Marius Bancila Mar 14 '11 at 13:19
6

buff is typed as a void pointer, which means it points to memory without declaring anything about the contents.

When you cast to char *, you declare that you're interpreting the pointer as being a char pointer.

Zr40
  • 1,910
  • 18
  • 20
4

In well written C++, you should not use C-style casts. So your cast should look like this:

void *buff; 
char *r_buff = static_cast<char *>(buff);

See here for an explanation of what the C++ casting operators do.

Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • Rewriting to use new-style casts rarely improves code much. In most cases, your goal should be to eliminate the cast entirely (and that goal can *usually* be met). – Jerry Coffin Mar 14 '11 at 14:44
1

By its name, buff is likely to be a memory buffer in which to write data, possibly binary data.

There are reasons why one might want to cast it to char *, potentially to use pointer arithmetic on it as one is writing because you cannot do that with a void*.

For example if you are supplied also a size (likely) and your API requires not pointer and size but 2 pointers (begin and end) you will need pointer arithmetic to determine where the end is.

The code could well be C in which case the cast is correct. If the code is C++ though a static_cast is preferable although the C cast is not incorrect in this instance. The reason a static_cast is generally preferred is that the compiler will catch more occasions when you cast incorrectly that way, and it is also more easily greppable. However casting in general breaks type-safety rules and therefore is preferably avoided much of the time. (Not that it is never correct, as it may be here).

CashCow
  • 30,981
  • 5
  • 61
  • 92