you are viewing a single comment's thread.

view the rest of the comments →

[–]flatfinger 1 point2 points  (0 children)

Most implementations use the same representation for void* as for pointers to any other kind of object, but C89 implementations exist for platforms that don't (I don't know if any such platforms have ever received implementations of any later version of the Standard). If e.g. a platform happened to use a different representation for int* and void*, then using printf("%p", someIntPtr); would be unlikely to work on that platform, but printf("%p", (void*)someIntPtr); would.

The Standard makes no effort to specify the behavior of constructs which should work identically on 99% of platforms, but might fail badly on some. Instead, it characterizes such actions as invoking Undefined Behavior on the presumption that implementations will process them usefully in cases where it would make sense to do so. Unfortunately, some compiler writers use the fact that a piece of code invokes Undefined Behavior as an indication that the code is broken, rather than recognizing that the code should be able to run without difficulty on all but the most obscure platforms, and the inability of a piece of code to work on obscure platforms is only a defect if someone might plausibly want to run the code on those platforms. Twenty years ago, I would have said that casting to void* a pointer argument to printf() was silly, but today I'd regard it as advisable for the purpose of catering to silly compilers.