casts

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.
 
N

Nick Keighley

A poor example, since this requires a mandatory diagnostic message.

exceedingly poor!

This is closer to what I had in mind

char f (char *s)
{
char c;
c = (char)s;
return c;
}
 
S

spinoza1111

But generally the comparison routine is trivial.

Doesn't mean it might not be Mung the Merciless. One of C's
"strengths" is how much damage it can do in a few lines of code. It
appeals to what British Marxist historian Eric Hobsbaum calls "the
anarchism of the lower middle class".
qsort, in practise, works well, and better than schemes using overloaded
comparison operators and the like which make it difficult to sort on more
than one criterion (eg in ASCII order or with embedded numbers in ascending
order).

Check out compareable in CSharp, please.
The weakness is when you want user to specify his own sort function, for
instance in  a high-level scripting language. The only solution, which isn't
ideal, is to have a global sort_script pointer, which the comparison
function can then use to specify the order.
This illustrates a general point with fucntion pointers. They should always
take an arbitrary void *as a parameter to pass back local data. qsort()
neglects to do this.

Duck soup if you can Inherit. But in C inherit can you not. The tool
has to be recreated. Oooooo I get to write a sort routine...beats
working...hmm lets look at wikipedia...wow check out Sonia's JUGS...ok
back to sort...hmm... bubble sort...sounds great!
 
T

Tom St Denis

I met Brian Kernighan at Princeton in 1987, and I told him that I'd
admired his early work in programming style. However, I was not at all
impressed with Ritchie's nonworking "regular expression" code in
Beautiful Code, because it showed a failure to understand twenty-plus
years of work in software, esp C Sharp and Java.

Neither C# nor Java existed in 1987. So you could hardly have pointed
out how flawed they are by comparison to K in 1987.

That being said, this is why you write libraries. Write a library to
handle your mundane tasks once, then re-use it whenever. Why people
like you insist that every time you encounter a string you have to re-
code the code to handle it by hand every time is beyond me.

And most people don't code in C because it's all "Mavericky" [to abuse
American Politico talk] but because it's portable, common place, well
understood [by the people who are actually professional about it], and
works. We write libraries to handle the mundane work and focus our
energies on higher level application details.

I have no idea what your goal is by ranting here but I think I speak
for a lot of others when I ask you to just stop. We get it, you don't
like Richard. Big deal. I don't know Richard either, nor are we
friends. That being said, you can still act civil about it. If you
want to flame Richard do so via e-mail. Usenet is not your personal
vehicle to flame people you dislike. And frankly, get a damn life.
There are way cooler things to do in life than flame people on
usenet. Go cycling, camping, see a movie, travel, anything but sit at
your computer and troll people morning to night. Seriously, give it a
rest.

Tom
 
N

Nick Keighley

Bill Cunningham said:
I seriously doubt, [although] I could be wrong, that learning just
from kandr2 one could get as good at code such as yourself or Ben
Pfaff. Do you agree? I don't know whether or not to get into kandr2
or start with C unleased.

"C Unleashed" is not for beginners. I recommend you stick with K&R2
for the time being (if you really wish to persist with this). Until
you've mastered K&R2, you're not going to get much, if any, value
from "C Unleashed", because it works from the premise that you
already have a reasonably solid knowledge of C - and, without wishing
to discourage you in any way, quite frankly you do not have that
level of C knowledge yet.

Which does raise an interesting issue. What is the next thing after
K&R?
If you've read ***and done all the excercises*** in K&R you've got a
pretty
good grounding in C. You've covered the whole of the language. apart
from learning the more obscure corners of the library and some C
pifalls
thats pretty much it. Pick an application domain you're interested in
writing programs for and write lots of programs. Read the clc FAQ.
Ask questions here.

Books to consider
The Standard C Library by Plauger
Harbission and Steel
Algorithms by Sedgewick


--

You are in a clearing. You can see a spire in the distance.
You can also see a copy of "C Unleashed".
: INV
You have;
a DeathStation 900 laptop,
a voucher for a free pizza,
and a torch.
: TAKE BOOK
You can't. It's too heavy.
Bill Godfrey (clc)
 
C

Curtis Dyer

Bill Cunningham said:
I seriously doubt, [although] I could be wrong, that learning
just from kandr2 one could get as good at code such as
yourself or Ben Pfaff. Do you agree? I don't know whether or
not to get into kandr2 or start with C unleased.

You can read all the books you want, but until you start writing a
lot of code, it probably won't sink in completely. Reading and
writing lots of code is an essential complementary process to
reading good books.

Perhaps one way to mitigate the memory issues you've mentioned might
be keeping a notebook or something near by. Everytime you
experience one of those "ah ha!" moments, you can make a concise
note that makes sense to you for future reference.

After having finally managed get a decent grasp of C, I have found
"C Unleashed" very helpful.

The best way I can describe the difference between K&R2 and "C
Unleashed" is that K&R2 is the best way for learning how to set up
an asel, use the paint brush, use the paint, etc. But once you
finally grasp how to use them, you end up with some basic tools and
a blank space on which you can paint almost anything. So, from
there, it can be intimidating for some to find direction without
already having specific tasks in mind. It is here where "C
Unleashed" has helped me most.
Which does raise an interesting issue. What is the next thing
after K&R? If you've read ***and done all the excercises*** in
K&R you've got a pretty good grounding in C. You've covered the
whole of the language. apart from learning the more obscure
corners of the library and some C pifalls thats pretty much it.
Pick an application domain you're interested in writing
programs for and write lots of programs. Read the clc FAQ. Ask
questions here.

I think the order of learning you present here is helpful. The
first time I tried to learn C, I ended up (inadvertently) reading
more C++ information than C. I also made the mistake of trying to
learn the Win32 API before having a firm understanding of C.

<snip>
 
A

arnuld

Necessary?  No:

  int *m = ...;
  void *m_void = m;
  printf("Memory allocated: %p\n", m_void);

and when I am going to use , I have to cast it back to the type it was
supposed to be. ?

--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
test.c: In function ‘main’:
test.c:4: warning: control reaches end of non-void function
 
B

Ben Bacarisse

arnuld said:
and when I am going to use , I have to cast it back to the type it was
supposed to be. ?

It is (presumably) and 'int *' so you'd use m with no cast. The
example is just a way to show that no cast is needed even in the case
of printing pointer.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
test.c: In function ‘main’:
test.c:4: warning: control reaches end of non-void function

It's a C99 program. Try adding -std=c99. I get no warnings in that case.
 
B

Ben Bacarisse

superpollo said:
Ben said:
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
test.c: In function ‘main’:
test.c:4: warning: control reaches end of non-void function


It's a C99 program. Try adding -std=c99. I get no warnings in that case.

[email protected]:~/test$ gcc -ansi -pedantic -Wall -std=c99 test.c
test.c: In function `main':
test.c:4: warning: control reaches end of non-void function
[email protected]:~/test$ gcc --version
gcc (GCC) 3.3.5 (Debian 1:3.3.5-8)

OK. A compiler can issue any warnings it likes. More recent gcc
versions have chosen remove this one for main since C99 defines
exactly what the program means by "reaching the end" of main.

BTW, my documentation says that -ansi and -std=c99 mean different
(inconsistent) things. It may be simply that my version (4.3.3) takes
the latter one rather the former but, either way, I get no warning.
 
S

spinoza1111

Neither C# nor Java existed in 1987.  So you could hardly have pointed
out how flawed they are by comparison to K in 1987.

I didn't do so, Reading Rainbow: I exchanged pleasantries and thanked
Brian Kernighan for his work on programming style. It was only after
the release of his essay in Beautiful Code from O'Reilly that I
realized he'd stayed married to the wrong girl.
That being said, this is why you write libraries.  Write a library to
handle your mundane tasks once, then re-use it whenever.  Why people
like you insist that every time you encounter a string you have to re-
code the code to handle it by hand every time is beyond me.

But I DON'T have to do this in C Sharp: where did you get the idea?

Writing one's own library means that your end user is forced to live
with it. It's a bad choice in most cases.
And most people don't code in C because it's all "Mavericky" [to abuse
American Politico talk] but because it's portable, common place, well

Did I reply to this before? C is NOT portable since only certain
styles of C port well at all.
understood [by the people who are actually professional about it], and
works.  We write libraries to handle the mundane work and focus our
energies on higher level application details.

If you write libraries to handle the mundane work, how can you focus
your energy elsewhere? Seems to me that you are doing one or the other
(or cleaning up the library). I've got an idea! How about a language
with a string data type that's able to handle Unicode as part of the
language? Wowie zowie, right?

Are you qualified to document your string library?

I have no idea what your goal is by ranting here but I think I speak
for a lot of others when I ask you to just stop.  We get it, you don't

We must speak for the herd, mustn't we.
like Richard.  Big deal.  I don't know Richard either, nor are we
friends.  That being said, you can still act civil about it.  If you

I've said it before and I will say it again. People here are quietly
destroyed by the resident thugs, and when they as men defend
themselves, little boys who can't defend themselves in relationships
or on the job blame the victim, without doing anything more than a
cursory scan of the most recent posts, the top of the stack if you
please, to find the victim defending himself. Not wishing to identify
with the victim, the poster assumes he or she must be to blame.

want to flame Richard do so via e-mail.  Usenet is not your personal
vehicle to flame people you dislike.  And frankly, get a damn life.
There are way cooler things to do in life than flame people on
usenet.  Go cycling, camping, see a movie, travel, anything but sit at
your computer and troll people morning to night.  Seriously, give it a
rest.

I will do so when Heathfield does.
 
T

Tom St Denis

But I DON'T have to do this in C Sharp: where did you get the idea?

Writing one's own library means that your end user is forced to live
with it. It's a bad choice in most cases.

Someone has to write the library though, and if nobody who uses C#
does what you need, guess what you have to write it yourself.

What? Do you think libraries don't exist for C? I don't get your
rant. I'm sure I can find all sorts of string, regex, etc, libraries
for C [GNU regex comes to mind]. Do you think every developer in C
writes everything from scratch?
And most people don't code in C because it's all "Mavericky" [to abuse
American Politico talk] but because it's portable, common place, well

Did I reply to this before? C is NOT portable since only certain
styles of C port well at all.

When I was in college working on my OSS crypto libraries I routinely
got reports of it working, out of the box, without changes, on
platforms I never encountered before. I was developing [at the time]
in Cygwin on an XP box, and I had users working on Playstation2 boxes,
HP-UX boxes, MIPS routers, etc, etc.

Where you get the idea that C programs are not portable I don't get.
If you write libraries to handle the mundane work, how can you focus
your energy elsewhere? Seems to me that you are doing one or the other
(or cleaning up the library). I've got an idea! How about a language
with a string data type that's able to handle Unicode as part of the
language? Wowie zowie, right?

Well, I'd just link in an EXISTING library that suited my needs. ...
Maybe I'm not understanding your point.

Is your entire rant because C doesn't have a built in strings C++
style class? So you hate all that is to do with C because it doesn't
have one facet?
Are you qualified to document your string library?

I'd like to think so. I write developer libraries for a living.
Hint: try working with ASN.1 data "natively" in C#. Tell me how that
works out for you.

<snip delusions>

Your persecution is all in your head. I don't see anyone here
starting threads that libel you or defame you. In fact, if anything
all the chatter about you seems to be in response to your inflammatory
posting style.

Tom
 
S

spinoza1111

But I DON'T have to do this in C Sharp: where did you get the idea?
Writing one's own library means that your end user is forced to live
with it. It's a bad choice in most cases.

Someone has to write the library though, and if nobody who uses C#
does what you need, guess what you have to write it yourself.

What?  Do you think libraries don't exist for C?  I don't get your
rant.  I'm sure I can find all sorts of string, regex, etc, libraries
for C [GNU regex comes to mind].  Do you think every developer in C
writes everything from scratch?

....and different regex packages give different answers for the SAME
regular expression. Infantile programmers have in fact destroyed
regular expressions as a mathematical formalism because there has been
no commercial support for standardization of regular expressions per
se.

This means that it's easier, in C, to rewrite the library than to
determine whether the pre-existing library works properly, and this is
a major defect in C.

What "really" constitutes a string in C? There is no correct answer.
If the primitives starting in str are used, then "a C string cannot
contain a Nul" is a true statement. But if mem* functions are used, or
some pretentious clown's library, then this is untrue.

But in mathematics there are correct answers. This is why it appears
that Dijkstra could never even bring himself to speak of C. C has
destroyed mathematics as a haven for truth.

The language, in fact, becomes a mechanism for turning good people
into Pretentious or Evil clowns because it is not a mathematical
formalism, and nothing is true or false. Instead, truth becomes a
popularity contest. Its ersatz becomes the politics of personal
destruction.

Cf. http://spinoza1111.wordpress.com/20...many-systems-for-parsing-regular-expressions/
And most people don't code in C because it's all "Mavericky" [to abuse
American Politico talk] but because it's portable, common place, well
Did I reply to this before? C is NOT portable since only certain
styles of C port well at all.

When I was in college working on my OSS crypto libraries I routinely
got reports of it working, out of the box, without changes, on
platforms I never encountered before.  I was developing [at the time]
in Cygwin on an XP box, and I had users working on Playstation2 boxes,
HP-UX boxes, MIPS routers, etc, etc.

Where you get the idea that C programs are not portable I don't get.
If you write libraries to handle the mundane work, how can you focus
your energy elsewhere? Seems to me that you are doing one or the other
(or cleaning up the library). I've got an idea! How about a language
with a string data type that's able to handle Unicode as part of the
language? Wowie zowie, right?

Well, I'd just link in an EXISTING library that suited my needs.  ...
Maybe I'm not understanding your point.

Is your entire rant because C doesn't have a built in strings C++
style class?  So you hate all that is to do with C because it doesn't
have one facet?
Are you qualified to document your string library?

I'd like to think so.  I write developer libraries for a living.
Hint: try working with ASN.1 data "natively" in C#.  Tell me how that
works out for you.

<snip delusions>

Your persecution is all in your head.  I don't see anyone here
starting threads that libel you or defame you.  In fact, if anything
all the chatter about you seems to be in response to your inflammatory
posting style.

Tom
 
T

Tom St Denis

...and different regex packages give different answers for the SAME
regular expression. Infantile programmers have in fact destroyed
regular expressions as a mathematical formalism because there has been
no commercial support for standardization of regular expressions per
se.

This means that it's easier, in C, to rewrite the library than to
determine whether the pre-existing library works properly, and this is
a major defect in C.

Even if that were true, it's a failure of the developers writing
libraries, not the language itself.

Also regex correctness has nothing to do with "mathematical formalism"
as the logic that prescribes how grammars work have nothing to do with
the field. At most it touches on set theory, but really compiler
theory is field of study on its own.

So if you're gonna rant, know what you're talking about.
What "really" constitutes a string in C? There is no correct answer.
If the primitives starting in str are used, then "a C string cannot
contain a Nul" is a true statement. But if mem* functions are used, or
some pretentious clown's library, then this is untrue.

Why would you want unprintable characters like \0 in a string
anyways?

And C DOES define what string literals are. So there is mention of
strings in C at least.
But in mathematics there are correct answers. This is why it appears
that Dijkstra could never even bring himself to speak of C. C has
destroyed mathematics as a haven for truth.

Have you actually spoken with Dijkstra about this? Or is this another
position you're inventing to serve your juvenile delusions of
grandeur?

I also like how you didn't respond to my reply about your comment that
C code isn't portable. Guess you're gonna put that one aside for a
rainy day eh?

Look, it's really simple, if you don't like C, code in something
else. At the very least, take your attitude and gross ignorance and
take off eh.

Tom
 
K

Keith Thompson

Tom St Denis said:
And C DOES define what string literals are. So there is mention of
strings in C at least.
[...]

String literals and strings are two very different things (both
unambiguously defined in the standard, of course).
 
L

luserXtrog

...and different regex packages give different answers for the SAME
regular expression. Infantile programmers have in fact destroyed
regular expressions as a mathematical formalism because there has been
no commercial support for standardization of regular expressions per
se.

Are you trying to put advertisements in to programming documentation?

<deletia>
 
N

Nick Keighley

Are you trying to put advertisements in to programming documentation?

no, he's trying to get regexps standardised. The fact is part of the
force
for standardisation is its perceived commercial utility.
 
S

spinoza1111

Even if that were true, it's a failure of the developers writing
libraries, not the language itself.

No, it's a failure of the language. I take it as axiomatic that when
smart people do poorly in a programming language, and dull fellows do
well, the language is defective.
Also regex correctness has nothing to do with "mathematical formalism"
as the logic that prescribes how grammars work have nothing to do with
the field.  At most it touches on set theory, but really compiler
theory is field of study on its own.

Oh? So it can make its own rules? I suggest you read Aho/Sethi/Ullman.
So if you're gonna rant, know what you're talking about.

I'm the author of a book on applied compiler design. I think I know
what I'm talking about.
Why would you want unprintable characters like \0 in a string
anyways?

Eye roll. Crotch grab.
And C DOES define what string literals are. So there is mention of
strings in C at least.

Wowie zowie. String literals. Just like Fortran-II. I'll alert the
media.
Have you actually spoken with Dijkstra about this?  Or is this another
position you're inventing to serve your juvenile delusions of
grandeur?

The complete Dijkstra repository of all his proto-blogs is available
at a Univ of Texas Web site. I have found no mention of C there.
I also like how you didn't respond to my reply about your comment that
C code isn't portable.  Guess you're gonna put that one aside for a
rainy day eh?

I have already addressed this issue. Where x is an arbitrary C program
whose internals are at time t unknown, a C expert E must be retained
to read the C code. This is not true for higher-level languages.
Look, it's really simple, if you don't like C, code in something
else.  At the very least, take your attitude and gross ignorance and
take off eh.

No, you take off, hoser. The defects of C are on-topic, far more so in
fact than the defects of people, a favorite topic here.
 
S

spinoza1111

spinoza1111wrote:
What "really" constitutes a string in C? There is no correct answer.
If the primitives starting in str are used, then "a C string cannot
contain a Nul" is a true statement. But if mem* functions are used, or
some pretentious clown's library, then this is untrue.

No.

N869
        7.  Library
        7.1  Introduction
        7.1.1  Definitions of terms
        [#1]  A  string  is  a  contiguous  sequence  of  characters
        terminated by and including the first null  character.

And you're still stupid.

That is NOT what a string is. A string is a series of symbols used by
humans to communicate with each other and store data in computers.

Glad to see you apostrophized "you are" correctly. One would not like
to appear to be the emotional fourteen year old you are by posting
that old Internet chestnut, "your stupid" to mean "thou are stupid".
 
N

Nobody

...and different regex packages give different answers for the SAME
regular expression. Infantile programmers have in fact destroyed
regular expressions as a mathematical formalism because there has been
no commercial support for standardization of regular expressions per
se.

This means that it's easier, in C, to rewrite the library than to
determine whether the pre-existing library works properly, and this is
a major defect in C.

This has nothing to do with C; regexp libraries behave the same way
regardless of which language you use them from. At least in C, the regexp
functions are just library functions, not an integral part of the
language. In fact, the balkanisation of regexps has a lot to do with
various high-level languages adding their own set of extensions.

So far as destroying regexps as a "mathematical formalism" goes, the
rot started with POSIX, which specified the use of \1, \2, ... within
regular [sic] expressions, resulting in the grammar ceasing to be regular.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top