Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #147178
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: How to use a struct with qsort()? |
| Date | 2019-10-23 13:51 -0700 |
| Organization | None to speak of |
| Message-ID | <lnwocvchj3.fsf@kst-u.example.com> (permalink) |
| References | (4 earlier) <BkKqF.69070$1A2.65981@fx07.iad> <r3MqF.740723$VT3.148620@fx19.am4> <lnftjoe4xu.fsf@kst-u.example.com> <865zkfhbn8.fsf@linuxsc.com> <4w_rF.736155$CO2.65319@fx30.am4> |
Bart <bc@freeuk.com> writes:
> On 23/10/2019 13:47, Tim Rentsch wrote:
>> Keith Thompson <kst-u@mib.org> writes:
>>
>>> Bart <bc@freeuk.com> writes:
>>>
>>>> [...] Use a typedef to give the struct its own name, so that you
>>>> don't have to write 'struct words' everywhere, eg:
>>>>
>>>> typedef struct {char *word; double wordpf;} Words;
>>>>
>>>> Now you can write
>>>>
>>>> Words **wordrec = NULL;
>>>>
>>>> or sizeof(Words) etc.
>>>
>>> That's a perfectly valid choice. It has the advantage that the
>>> name of the type is a single identifier.
>>>
>>> Personally, though, I prefer to refer to the type as "struct
>>> whatever" rather than inventing a new name for it. My point of
>>> view is that if you define a struct with a tag, it already has a
>>> perfectly good name. [...]
>>
>> If a struct is defined with a tag, there is no doubt that the
>> type already has a name. Whether that name is a perfectly good
>> name is another question altogether.
>
> 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.
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.
> And given 'struct T', that T could exist in the same scope as an
> unrelated type 'T'. Further, you can choose to do one of:
>
> struct T T;
>
> or:
>
> typedef struct T T;
>
> or both:
>
> struct T T;
> typedef struct U U;
>
> So struct T and the variable T have the same name, sort of; and struct U
> and the type U have the same name, sort of, and here can be used
> interchangeably.
Yes, you can create a mess if you use the identifiers "T" and "U" for
everything. That's why it's a good idea to use more descriptive names.
The problem in your examples isn't the "struct" keyword, it's the short
and non-descriptive names you chose. (Names like "T" are fine for
non-specific examples, but are rarely a good idea in real life.)
> This is why it might be best to forget the possibility of struct tags
> altogether, and always define a typedef for a struct.
That requires a little extra work if a struct contains a pointer to
itself. For example, you can have the typedef *before* the full struct
definition:
typedef struct node node;
struct node {
int data;
node *next;
};
Or you can refer to the type as "struct node" inside its definition
and refer to the typedef elsewhere:
typedef struct node {
int data;
struct node *next;
} node;
> 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.
You can use typedef to create such names if you like, and a lot of
programmers do so. I'm not saying there's anything wrong with that.
If you think it's a great advantage for your types to have names
that are single identifiers, I'm not even saying you're wrong.
It's just not my preference. And if you're going to be reading C
code written by others, you'll need to cope with both styles.
> 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.)
--
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
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