Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
This was inspired by the SO question Can you ever assume typecasting pointers is safe? I started to write an answer there, but I realized I'd rather post it here instead, so here's a self-answered ...
#1: Initial revision
Can you ever assume that casting pointers is safe?
This was inspired by the SO question [Can you ever assume typecasting pointers is safe?](https://stackoverflow.com/questions/20402814/can-you-ever-assume-typecasting-pointers-is-safe) I started to write an answer there, but I realized I'd rather post it here instead, so here's a self-answered Q&A. --- **Can you ever assume that casting pointers is safe?** Casting implies the use of the `( )` cast operator. As soon as it is used, C seems to allow all manner of wild and crazy casts and they pass compilation, but are those actually fine? How to know which ones that are and which ones that aren't? Even when restricting oneself only to casts involving pointers, there are surely a lot of different scenarios here. As seen in the linked question, it is suggested that the size of the pointer and data types matter, which is rather obvious. Alignment might be less obvious, and on top of that C comes with a bunch of specialized rules such as exceptions for character type pointers, the "strict pointer aliasing" rules, function pointers and so on. What is safe, what is unsafe? Is there an exhaustive list of all safe/dangerous scenarios? What does the standard specify and what does it leave as "poorly-defined behavior" such as undefined behavior?
