Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: How to use a struct with qsort()? Date: Sat, 19 Oct 2019 15:38:53 -0700 Organization: None to speak of Lines: 64 Message-ID: References: <06add10e-1e30-4741-a20c-ff56363733fe@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="135486503aa1822288de33256f1f1eef"; logging-data="7656"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/dw54R8aJbZnGvATkm6pOd" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:pZYLlJi+NHLgyW9Dcgg+WEyu0R0= sha1:sOF75gYyouKCTmddrokzdXTYwTU= Xref: csiph.com comp.lang.c:147097 Bart writes: > On 19/10/2019 21:26, DFS wrote: >> On 10/19/2019 4:09 PM, already5chosen@yahoo.com wrote: >> ============================================================= > >> struct words {char *word; double wordpf;}; >> struct words **wordrec = NULL; > > Some suggestions that may or may not help: > > (1) 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. I don't see much advantage in inventing another name for it. If you do choose to use a typedef in addition to the tag name, there's no real reason not to use the same identifier for both. (Or you can omit the tag name, but only for structures that don't contain a pointer to the same type). And since the type represents a single word, the name "words" or "Words" is confusing. I might write: struct word {char *str; double wordpf;}; (I don't know what "wordpf" represents). Note that I've used a different identifier for the char* member. Or you could write: typedef struct {char *str; double wordpf;} word; if you prefer a name that's a single identifier. (And if a word contains a pointer to a word, you'll need "typedef struct word ...", and you'll have to refer to it as "struct word" inside the definition.) > (2) Use capitalised names for your typedefs as above, then it might be > easier to see what is a type, and what is a variable name, which can > cause confusion. Not a bad convention, but I haven't seen it used widely. It could cause some confusion if you mix code that uses that convention with code that doesn't. Picking good and unambiguous names is hard, but it's worth the effort. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */