S
spinoza1111
spinoza1111said:
Fred said:
On Jul 30, 10:58 am, Antoninus Twink <[email protected]>
wrote:
On 30 Jul 2009 at 14:37, Bill Cunningham wrote:
Something Richard Heathfield said once very much sticks out in
my mind. If code is written properly there is no need (or
rarely) for casts.
As with most things Heathfield says, this needs to be taken with
a pinch of salt. As is typical for Heathfield, this statement is
pure polemic to push his "Sola ISO C" fundamentalism.
There are many places where casts are essential: for example, to
implement polymorphism in object-oriented C programming or for
type punning. There are also many places where casts are useful:
for example, casting a uint64_t to a uint32_t is a simple way to
reduce it mod 2^32.
Also, it is difficult to use qsort without using cast.
No, it's easy (see below).
Don't make the mistake of believing Antoninus Twink. Casts are
almost never a good idea, and the places where they are a good idea
are not the places you'd think.
Here is an example of using qsort without a cast:
#include <stdlib.h> /* for qsort */
#include <string.h> /* for strcmp, used in comparison function */
struct bigramfreq_
{
char bigram[3];
unsigned long freq;
};
typedef struct bigramfreq_ bigramfreq; /* the things we're sorting
*/
/* comparison function */
int compbigramfreq(const void *vp1, const void *vp2)
{
const bigramfreq *p1 = vp1;
const bigramfreq *p2 = vp2;
int diff = (p1->freq < p2->freq) - (p1->freq > p2->freq);
if(diff == 0)
{
diff = strcmp(p1->bigram, p2->bigram);
}
return diff;
}Since I no longer use C, regarding its use as criminal save for
recreational programming, I may have missed something here, but this
code is to me globally incompetent...so incompetent, in fact, as to
relabel competence itself and normalize deviance.
You've missed something here.
Yes, I did. You weren't advocating weak typing for the hell of it. I
thought you were, but you're not incompetent in the small, you're
globally incompetent because you don't understand the last thirty
years of software science. This is because you have refused to learn
new paradigms. If software were a proper profession you'd have lost
your license to practice it some time ago, especially in view of the
way you attack perfectly reasonable tests when you fail them.
Somebody asked how to do qsort without casting and you showed them.
However, qsort is the problem and you nonetheless use it. C cannot
sort at all in a safe sense, because "to sort" is a class of problems
and not a single problem, that requires a set of objects and an
ordering relation. C is incapable of expressing this level of
abstraction, except by allowing the coder of a sort rule to accept
nonsense parameters without giving him the opportunity to see if they
point somewhere sensible.