Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #147180

Re: How to use a struct with qsort()?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: How to use a struct with qsort()?
Date 2019-10-23 15:50 -0700
Organization None to speak of
Message-ID <ln8spbcc0p.fsf@kst-u.example.com> (permalink)
References (6 earlier) <lnftjoe4xu.fsf@kst-u.example.com> <865zkfhbn8.fsf@linuxsc.com> <4w_rF.736155$CO2.65319@fx30.am4> <lnwocvchj3.fsf@kst-u.example.com> <5b4sF.1834564$HWr.1450852@fx36.am4>

Show all headers | View raw


Bart <bc@freeuk.com> writes:
> On 23/10/2019 21:51, Keith Thompson wrote:
>> Bart <bc@freeuk.com> writes:
>>> It doesn't have a name that is a pure identifier, such as T. You have to
>>> refer to it by the composite 'struct T'.
>> 
>> Correct.  And the types `int*` and `char[20]` also don't have names that
>> are pure identifiers.
>
> Sure, if you're happy using an actual type constructor, which can be 
> used to describe any composite type, and are happy to have to repeat the 
> exact same constructor in multiple locations.
>
> My post is about named user-types, where the details of the underlying 
> type are given in only one place.
>
>> Most C type names are not pure identifiers.  Most of them are
>> keywords (int), or sequences of keywords (unsigned long), or various
>> combinations of keywords, identifiers, and punctuation symbols
>> (char **).  In the absence of typedefs and macros, C type names
>> are *never* pure identifiers.
>
> My post is about naming types. In the case of a struct, C also allows 
> the untidy practice of having anonymous structs like struct{int a,b;}, 
> and the half-way house of assigning a struct tag like struct T{int a,b;}.
>
> (And, since you appear to have trouble with this, T is not meant to 
> represent a meaningful struct tag.)

I have no trouble with it.  Apparently you agree with me that T is not
generally a good name for a type or a tag.

One of your examples was
    struct T T;
It could have been
    struct COFF_Reloc_Rec COFF_Reloc_Rec;
Do we agree that using the same identifer for a struct tag and for an
object of the struct type is a bad idea?  Or for a typedef name and an
object of that type?  That's what you did in your example.  Perhaps a
longer name would have made that clearer.

I don't recall anyone suggesting using the same name for a type and
for an object of that type.  You did so in your example.  If that
wasn't your intent, I'll accept that -- but then I'm not sure what point
you were making.

> In my opinion, struct tags are a useless appendage to C, and if they 
> didn't exist, would be missed by nobody. They just generate the 
> possibility of extra confusion and more errors.
> 
> If people actually wanted to write 'struct T', then they can simply call 
> the type 'struct_T'.
> 
> (As you mention later, at present C may require struct tags in certain 
> cases where you are constructing pointers to the struct. That is a 
> language limitation (which would not exist if tags didn't exist), and in 
> that case tags have to be used, preferably a tag name chosen to indicate 
> it is just for that purpose.)

I understand that that's your preference.  I've already acknowledged
that it's a valid preference.  You refuse to do the same for my own
preference.

[...]

> Who said that my struct T T illustration should be used in real 
> programs? Maybe I should written:
>
>    struct COFF_Reloc_Rec COFF_Reloc_Rec;
>
> Do you think that would have helped me put my point across, or hindered 
> it? Are those two identifiers exactly the same (look carefully)? T and T 
> are clearly the same.

Yes, they're exactly the same.  Exactly what point are you making with
that declaration?

>>> Then the name of the struct type will be a regular identifier like other
>>> user types and the tag cannot clash with either other user types or
>>> variables
>> 
>> Again, most other user types don't have names that are single
>> identifiers, unless you create such names explicitly.
>
> And again, I'm talking about named types, not anonymous ones. And named 
> types (ie. types with associated user-defined identifier) is the context 
> I was replying to.

Yes, I understand that.  "int*" is not an anonymous type.  It has a
name, and that name is "int*".  (See the definition of "type-name" in
the C syntax.  It's not just an identifier.)

[...]

> As it happens, I don't use single letter type names for real programs, 
> ever. Only in posts here, or in test code, where extraneous matters need 
> to be minimised.

Great.  In this case, doing so may have unintentionally obscured your
point.

>>> It also helps to make the type more opaque, which in turn means you can
>>> change the underlying type without having to change 100s of 'struct T'
>>> to 'struct T*' or 'U'.
>> 
>> Typedefs are appropriate for types that actually need to be opaque
>> (see FILE, for example).  But if code that uses your type is going
>> to be referring to member names, there's no point in trying to hide
>> the fact that it's a structure.  (If you change it from a struct to
>> a struct pointer, you'll have to change "." to "->" in client code,
>> and likely make other changes.)
>
> Clearly, my example is for when you are not expected to access the 
> struct members.

That wasn't clear at all.  Now that you've stated that assumption, as I
already stated I agree that using a typedef to give a simple name to an
opaque type is a good idea.

> Notice how FILE is defined as FILE, and not 'struct FILE', or 'struct 
> FILE*'.

Yes.  Are you under the impression that that contradicts anything I've
said?  I explicitly stated that FILE is a good example of using a
typedef for an opaque type.

Now let's say we want to define a non-opaque type, for example the type
of a node in a linked list:

    struct node {
        int data;
        struct node *next;
    };

That's how I'd define it.  Client code needs to refer to the "data" and
"next" members, and thus depends on it being a structure.

You'd probably define it something like this:

    typedef struct node_ {
        int data;
        struct node_ *next;
    } node;

with the tag "node_" used to discourage using it outside the struct
definition.  (An alternative is to use "node" both as the typedef name
and the tag name.)

That's fine with me.  It's not my preference, but it's a perfectly valid
style that a lot of programmers use.

Do you, or do you not, acknowledge that the way I do it (no typedef,
referring to the type as "struct node") is also valid?  I'm not asking
whether you like it (I already know you don't).  I'm asking whether you
acknowledge that it's valid.

Note that there are several struct types defined in the C standard
library that have no associated typedefs (struct tm is one example).
In fact, I don't think there's *any* structure type in the standard
library for which the standard specifies both the names of its
members and a typedef for the type.  (No need to say that you dislike
the style used by the standard; I think we're all aware of that.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-19 13:35 -0400
  Re: How to use a struct with qsort()? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-10-19 10:22 -0700
    Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-19 14:19 -0400
      Re: How to use a struct with qsort()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-10-19 19:36 +0100
      Re: How to use a struct with qsort()? Melzzzzz <Melzzzzz@zzzzz.com> - 2019-10-19 18:44 +0000
  Re: How to use a struct with qsort()? Barry Schwarz <schwarzb@delq.com> - 2019-10-19 10:23 -0700
  Re: How to use a struct with qsort()? Ike Naar <ike@sdf.lonestar.org> - 2019-10-19 19:09 +0000
    Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-19 15:51 -0400
      Re: How to use a struct with qsort()? already5chosen@yahoo.com - 2019-10-19 13:09 -0700
        Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-19 16:26 -0400
          Re: How to use a struct with qsort()? already5chosen@yahoo.com - 2019-10-19 13:51 -0700
            Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-19 17:24 -0400
              Re: How to use a struct with qsort()? already5chosen@yahoo.com - 2019-10-19 14:49 -0700
                Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-19 18:26 -0400
                Re: How to use a struct with qsort()? already5chosen@yahoo.com - 2019-10-19 16:32 -0700
                Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-20 09:22 -0400
                Re: How to use a struct with qsort()? Paul <nospam@needed.invalid> - 2019-10-26 04:15 -0400
                Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-26 09:37 -0400
                Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-26 15:40 +0100
                Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-28 19:49 -0400
                Re: How to use a struct with qsort()? Paul <nospam@needed.invalid> - 2019-10-29 01:59 -0400
                Re: How to use a struct with qsort()? Paul <nospam@needed.invalid> - 2019-10-30 04:54 -0400
                Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-30 11:54 +0000
                Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-30 08:58 -0700
                Re: How to use a struct with qsort()? Öö Tiib <ootiib@hot.ee> - 2019-11-07 01:10 -0800
                Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-11-07 10:51 +0000
                Re: How to use a struct with qsort()? Paul <nospam@needed.invalid> - 2019-10-26 15:03 -0400
                Re: How to use a struct with qsort()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-10-26 21:50 +0100
                Re: How to use a struct with qsort()? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-04 16:02 -0800
            Re: How to use a struct with qsort()? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-10-21 11:58 +0000
            Re: How to use a struct with qsort()? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-10-23 04:51 -0700
          Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-19 23:25 +0100
            Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-19 15:38 -0700
              Re: How to use a struct with qsort()? David Brown <david.brown@hesbynett.no> - 2019-10-20 14:44 +0200
              Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-22 15:42 -0400
              Re: How to use a struct with qsort()? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-10-23 05:47 -0700
                Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-23 16:40 +0100
                Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-23 13:51 -0700
                Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-23 23:07 +0100
                Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-23 15:50 -0700
                Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-24 01:20 +0100
                Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-23 18:20 -0700
                Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-24 10:40 +0100
                Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-24 11:00 -0700
          Re: How to use a struct with qsort()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-10-20 00:30 +0100
            Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-20 00:44 +0100
          Re: How to use a struct with qsort()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-10-20 01:27 +0100
          Re: How to use a struct with qsort()? Manfred <noname@add.invalid> - 2019-10-25 20:30 +0200
            Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-25 16:07 -0400
            Re: How to use a struct with qsort()? Ike Naar <ike@sdf.lonestar.org> - 2019-10-26 07:27 +0000
              Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-26 02:11 -0700
            Re: How to use a struct with qsort()? Bart <bc@freeuk.com> - 2019-10-26 11:06 +0100
              Re: How to use a struct with qsort()? Manfred <noname@invalid.add> - 2019-10-26 16:58 +0200
                Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-26 18:17 -0400
                Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-26 16:08 -0700
                Re: How to use a struct with qsort()? DFS <nospam@dfs.com> - 2019-10-26 21:33 -0400
                Re: How to use a struct with qsort()? Keith Thompson <kst-u@mib.org> - 2019-10-26 22:10 -0700
                Re: How to use a struct with qsort()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-10-27 01:48 +0100
                Re: How to use a struct with qsort()? Manfred <noname@invalid.add> - 2019-10-27 19:29 +0100
                Re: How to use a struct with qsort()? Barry Schwarz <schwarzb4aarp@gmail.com> - 2019-10-27 13:09 -0700

csiph-web