logoalt Hacker News

amlutoyesterday at 3:21 PM1 replyview on HN

> Malloc returns a buffer and then you cast it to the type you want.

You can’t cast the buffer — you’re casting the pointer.

One might argue (I’m not sure whether this is correct) that malloc allocated an array of ints and the language merely has no way to state that directly. Then you write to those ints using a char pointer, and then you access them as ints, but they’ve been ints ever since allocation.


Replies

groundzeros2015yesterday at 3:28 PM

yes but that works not because malloc is special but because there are more relaxed types rules than suggested by the comment above.