clc style

R

richard

Two nice style points I've learned from clc

1) xyz = malloc(count * sizeof(*xyz)); rather than
xyz = malloc(count * sizeof(xyz-type));

2) TYPE xyz = {0}; rather than
memset(&xyz, 0, sizeof(xyz));
TYPE xyz = {0, 0, 0};
TYPE xyz = {NULL, 0, NULL};
if xyz is a struct: xyz.this = NULL; xyz.that = 0; xyz.other = NULL;

Any other analogous suggestions?
 
I

Irrwahn Grausewitz

richard said:
1) xyz = malloc(count * sizeof(*xyz)); rather than
xyz = malloc(count * sizeof(xyz-type));

Even better (xyz beeing a pointer-to-xyt_t):

xyz = malloc( count * sizeof *xyz ); rather than
xyz = malloc( count * sizeof (xyz_t) ); rather than
xyz = malloc( count * sizeof(xyz_t) );

[ See my reply to your question in the
"free(); after return();?"-thread ]

<SNIP>
 
P

Peter Nilsson

richard said:
Two nice style points I've learned from clc

1) xyz = malloc(count * sizeof(*xyz)); rather than

Or just...

xyz = malloc(count * sizeof *xyz);
xyz = malloc(count * sizeof(xyz-type));

2) TYPE xyz = {0}; rather than
memset(&xyz, 0, sizeof(xyz));

Going from strict conformance to undefined behaviour is not a style
difference.
TYPE xyz = {0, 0, 0};
TYPE xyz = {NULL, 0, NULL};
if xyz is a struct: xyz.this = NULL; xyz.that = 0; xyz.other = NULL;

Note that succinctness can also be obfuscory. Some prefer to explicitly show
all initialisers for readability. Which is better all depends on the
position of your tongue when writing code.
Any other analogous suggestions?

The group could no doubt offer thousands of suggestions, but we don't know
what it is you don't know!

Read your way through the FAQ and previous clc posts (e.g. via Google
groups). Lurk for a good while (say 12 months) and you'll pick up tidbits
from replies to the same queries which seem to get recycled in tune with the
student calendar...
 
R

richard

The group could no doubt offer thousands of suggestions, but we don't know
what it is you don't know!

Read your way through the FAQ and previous clc posts (e.g. via Google
groups). Lurk for a good while (say 12 months) and you'll pick up tidbits
from replies to the same queries which seem to get recycled in tune with the
student calendar...

I didn't see either of these in the FAQ. I found these two from lurking and
google, but haven't found anything similar.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
Two nice style points I've learned from clc

1) xyz = malloc(count * sizeof(*xyz)); rather than

ITYM:

xyz = malloc (count * sizeof *xyz);
Any other analogous suggestions?

Yes: remove the useless parens!
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
Two nice style points I've learned from clc

1) xyz = malloc(count * sizeof(*xyz)); rather than
xyz = malloc(count * sizeof(xyz-type));

2) TYPE xyz = {0}; rather than
memset(&xyz, 0, sizeof(xyz));
TYPE xyz = {0, 0, 0};
TYPE xyz = {NULL, 0, NULL};
if xyz is a struct: xyz.this = NULL; xyz.that = 0; xyz.other = NULL;

Any other analogous suggestions?

Ban of gets()
Good usage of assert()
Good usage of 'const' specially for pointers parameters.
etc.

If you read French, try the French C-FAQ:

FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
A

Alex

Is the French C FAQ a translation of the English C FAQ? If not,
would it be interesting to translate the French C FAQ into
English?

Babelfished first paragraph of the mentioned URL:

This document gathers the questions most frequently put (with
the answers) of the French-speaking newsgroup fr.comp.lang.c
about the language C. This FAQ (see question 2.1) is based on
that of comp.lang.c maintained by Steve Summit
(www.eskimo.com/~scs/C-faq/top.html).

Alex
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
Is the French C FAQ a translation of the English C FAQ?

It was inspired by the English C-FAQ, but was largely amended.
If not,
would it be interesting to translate the French C FAQ into
English?

Yes, probably.
 
R

Richard Heathfield

Ben said:
Is the French C FAQ a translation of the English C FAQ?

Sans acute accents, the page reads:

"Ce document regroupe les questions les plus frequemment posees (avec les
reponses) du groupe de discussion francophone fr.comp.lang.c sur le langage
C. Cette FAQ (voir la question 2.1) est basee sur celle de comp.lang.c
maintenue par Steve Summit (www.eskimo.com/~scs/C-faq/top.html)."

Babelfish obligingly does the honours...

"This document gathers the questions most frequently put (with the answers)
of the French-speaking newsgroup fr.comp.lang.c about the language C. This
FAQ (see question 2.1) is based on that of comp.lang.c maintained by Steve
Summit (www.eskimo.com/~scs/C-faq/top.html)."
 

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
474,079
Messages
2,570,573
Members
47,204
Latest member
MalorieSte

Latest Threads

Top