Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Naming convention for structs and types Date: Thu, 12 Jan 2017 13:01:18 -0800 Organization: None to speak of Lines: 67 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="768745921b79448f5a531b86aeb60d71"; logging-data="31379"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18by5ea/Nosd361I+aqqQp7" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:Qx6o5kheT9OcZjLzWbVBJTdn3cg= sha1:anRosvBPX9/2sFLDMStOQQiaL1E= Xref: csiph.com comp.lang.c:101552 "Fernando D. Bozzo" writes: > I made this struct: > > > typedef struct justfname_s > { > char *value; > int len; > } justfname_t; > > > and want to know if it's correct to end struct's names with "_s" and > types with "_t". In what I've read until now, I didn't see any clear > convention. Consistency is the most important thing. If you define a struct with both a tag name (the identifier following the "struct" keyword) and a typedef name, there's no requirement for them to differ. This is perfectly valid: typedef struct justfname { char *value; int len; } justfname; and lets you refer to the type either as "struct justfname" or as "justfname". On the other hand, using the same identifier for both might confuse some IDEs, particular search facilities. If you're going to use a typedef, you also need a tag if you want to refer to the type within its definition, particularly if the type contains a pointer to itself. You could write: typedef struct { char *value; int len; } justfname; but it's probably best to be consistent, using a tag whether there happens to be a pointer member or not. Finally, you don't have to use a typedef at all: struct justfname { char *value; int len; }; The type already has a perfectly good name, "struct justfname". Adding a typedef merely gives it a second name, "justfname". In my opinion, this saves a little typing, but that's not a good enough reason to give something two separate names -- and have to think about conventions for how those names relate to each other. The counterargument is that it's worthwhile for the type to have a name that's a single identifier. That's a perfectly valid position, and plenty of good programmers use typedefs for that reason. And C++ lets you refer to the type as "justfname" even without a typedef. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"