Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

84%
+9 −0
Q&A Can you ever assume that casting pointers is safe?

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 answer  ·  posted 2mo ago by Lundin‭  ·  last activity 2mo ago by Lundin‭

Question c pointers implicit-conversion conversions
#1: Initial revision by user avatar Lundin‭ · 2026-07-07T14:24:54Z (2 months ago)
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?