Re: Coding style survey

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 01/30/04


Date: Thu, 29 Jan 2004 19:06:04 -0500 (EST)


On Thu, 29 Jan 2004, Old Wolf wrote:
> [Arthur wrote:]
> > char c, *p, q, *x;
> >
> > or whatever it really was; I'm not going to take the time to look
> > back right now. I maintain that this is a Bad Idea.
>
> What do you think of
> char , *p[], q(), *x;
> then? :)

  Slightly better, in that the compiler will refuse to compile it,
rather than let it sit on your disk misleading future programmers. :)

> Recently I wrote some code like:
>
> const unsigned char s[] = {
> foo(1),
> foo(2),
> /* ... (about 10 entries) */
> }, *ps = s;
>
> so that later on I could iterate with ps, or use the results
> from the functioncalls in many places. Using this syntax
> avoided having to type out "const unsigned char" twice (or use typedef)

  Ooh, dear, typing 'const unsigned char' twice! ;-) Anyway, this
sort of thing is one of very few exceptions to the general rule.
I have seen and appreciated the idiom

  typedef struct foo { bar blah; } foo, *pfoo;

even though I would obviously prefer to write

  typedef struct foo foo;
  typedef foo *pfoo;
  struct foo { bar blah; };

in such situations, if they ever arose in my own code. And it's
possible to get borderline cases like the one you describe, even
in real code. But I'd still write

     const unsigned char s[] = {
         foo(1),
         foo(2),
         /* ... (about 10 entries) */
     };
     const unsigned char *ps = s;

in code meant for human consumption. The extra 'const unsigned char'
is quick to code, in an editor with cut-and-paste; and if you are
seriously concerned that 'unsigned char' may change in future versions,
you should be using a typedef anyway.

-Arthur



Relevant Pages

  • Re: Opaque pointers
    ... the typedef struct List_T * List_T ... void fookill(fooptr fooid); ... int fooinsert; ... user has no idea what is in a "struct foo", ...
    (comp.lang.c)
  • Re: incomplete types
    ... typedef struct foo foo_t; ... typedef struct foo *foo_ptr; ... pointer there in the typedef. ...
    (comp.lang.c)
  • Re: incomplete types
    ... typedef struct foo foo_t; ... typedef struct foo *foo_ptr; ... "pointerness" through the type's name. ... A FILE is an opaque type in that you are not permitted to know ...
    (comp.lang.c)
  • Re: Gfortran on slashdot front page
    ... C++ class) there may be only one full declaration per scope, ... except for typedef there may also be a tag-only forward declaration, ... the C++ added feature where 'struct foo' etc. makes foo a typename as ...
    (comp.lang.fortran)
  • Re: [releng_6 tinderbox] failure on sparc64/sparc64
    ... Write it out 1 unsigned char at a time for the simple version. ... nbytes = sizeof; ...
    (freebsd-arch)