Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: What is better and why?

Reply
Thread Tools

Re: What is better and why?

 
 
BartC
Guest
Posts: n/a
 
      06-10-2013
"paskali" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 1st:
>
> struct person {
> char *name;
> char *surname;
> };
>
> 2nd:
>
> typedef struct {
> char *name;
> char *surnname;
> } person;
>
> 3th:
>
> typedef struct person {
> char *name;
> char *surname;
> } person;
>
> 4th:
>
> typedef struct _PERSON {
> char *name;
> char *surnmae;
> } person;
>


I don't know which is considered better; it's just a matter of style:

I use variations of 2, 3 and 4. I like using a single identifier for the
name (it hides the fact that it is a struct, and also makes it easy to
change to something that isn't! And it makes type declarations more
compact).

I use a struct tag (as in 3 and 4) if a pointer to the type is one of the
elements:

typedef struct _person {
char* name;
char* surname;
struct _person* next;
} person;

The underscore "_" is optional; struct tag names don't clash with typedef
names (and for similar reasons, are unlikely to clash with reserved system
names). But of course the name can be anything at all.

However I use my own conventions for naming struct typedefs (so your
examples might be named rperson or personrec). It could be argued that this
isn't much different from using a 'struct' prefix anyway; I would say it's a
bit more different!

--
Bartc

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-10-2013
"BartC" <(E-Mail Removed)> writes:
[...]
> typedef struct _person {
> char* name;
> char* surname;
> struct _person* next;
> } person;
>
> The underscore "_" is optional; struct tag names don't clash with typedef
> names (and for similar reasons, are unlikely to clash with reserved system
> names). But of course the name can be anything at all.


7.1.3p1: All identifiers that begin with an underscore are always
reserved for use as identifiers with file scope in both the ordinary and
tag name spaces.

If the above declaration appears outside any function (which is the most
likely place for it), it could conflict with an implementation-defined
identifier "_person", either in a standard header or via compiler magic.

It's probably true that an implementation is very unlikely to define
"_person" for its own use -- but why settle for unlikelihood when you
can have certainty just as easily?

If you feel the need for a convention, you can always append an
underscore:

typedef struct person_ {
...
} person;

But as far as the language is concerned, no such "convention" is needed;
there's no reason at all to use distinct identifiers for the tag name
and typedef name.

Making the identifiers distinct might have some advantage for
non-language-defined tools (for example, it's easier to search for all
occurrences of "person_" than for all instances of "person" not
immediately following a "struct" keyword).

> However I use my own conventions for naming struct typedefs (so your
> examples might be named rperson or personrec). It could be argued that this
> isn't much different from using a 'struct' prefix anyway; I would say it's a
> bit more different!


--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
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"
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      06-10-2013
On 06/10/2013 02:00 PM, Keith Thompson wrote:
> "BartC" <(E-Mail Removed)> writes:
> [...]
>> typedef struct _person {
>> char* name;
>> char* surname;
>> struct _person* next;
>> } person;
>>
>> The underscore "_" is optional; struct tag names don't clash with typedef
>> names (and for similar reasons, are unlikely to clash with reserved system
>> names). But of course the name can be anything at all.

>
> 7.1.3p1: All identifiers that begin with an underscore are always
> reserved for use as identifiers with file scope in both the ordinary and
> tag name spaces.


"... each structure or union has a separate name space for its members
...." (6.2.3p1). They therefore do not conflict with names declared in
either the ordinary or the tag name space.

I think it's simpler to following a convention of avoiding the use of
all identifiers that start with an underscore; but use as struct or
union member names is safe.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-10-2013
James Kuyper <(E-Mail Removed)> writes:
> On 06/10/2013 02:00 PM, Keith Thompson wrote:
>> "BartC" <(E-Mail Removed)> writes:
>> [...]
>>> typedef struct _person {
>>> char* name;
>>> char* surname;
>>> struct _person* next;
>>> } person;
>>>
>>> The underscore "_" is optional; struct tag names don't clash with typedef
>>> names (and for similar reasons, are unlikely to clash with reserved system
>>> names). But of course the name can be anything at all.

>>
>> 7.1.3p1: All identifiers that begin with an underscore are always
>> reserved for use as identifiers with file scope in both the ordinary and
>> tag name spaces.

>
> "... each structure or union has a separate name space for its members
> ..." (6.2.3p1). They therefore do not conflict with names declared in
> either the ordinary or the tag name space.
>
> I think it's simpler to following a convention of avoiding the use of
> all identifiers that start with an underscore; but use as struct or
> union member names is safe.


The code used _person as a struct tag, not as a member name.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
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"
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      06-10-2013
On 06/10/2013 06:00 PM, Keith Thompson wrote:
> James Kuyper <(E-Mail Removed)> writes:
>> On 06/10/2013 02:00 PM, Keith Thompson wrote:
>>> "BartC" <(E-Mail Removed)> writes:
>>> [...]
>>>> typedef struct _person {
>>>> char* name;
>>>> char* surname;
>>>> struct _person* next;
>>>> } person;
>>>>
>>>> The underscore "_" is optional; struct tag names don't clash with typedef
>>>> names (and for similar reasons, are unlikely to clash with reserved system
>>>> names). But of course the name can be anything at all.
>>>
>>> 7.1.3p1: All identifiers that begin with an underscore are always
>>> reserved for use as identifiers with file scope in both the ordinary and
>>> tag name spaces.

>>
>> "... each structure or union has a separate name space for its members
>> ..." (6.2.3p1). They therefore do not conflict with names declared in
>> either the ordinary or the tag name space.
>>
>> I think it's simpler to following a convention of avoiding the use of
>> all identifiers that start with an underscore; but use as struct or
>> union member names is safe.

>
> The code used _person as a struct tag, not as a member name.


I need to get some more sleep. Sorry for the noise.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xmp, what to use instead of. myphplists@yahoo.com HTML 9 04-29-2013 03:35 PM
what is the advantage of using maven for java standalone app mcheung63@gmail.com Java 13 04-16-2013 01:42 AM
What Linux freeware will blur faces & show all frames of a 30second AVI video? Danny D. Digital Photography 8 04-14-2013 10:18 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM



Advertisments