Velocity Reviews - Computer Hardware Reviews

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

Thread Tools

Re: What is better and why?

Lew Pitcher
Posts: n/a
On Sunday 09 June 2013 16:57, in comp.lang.c, Removed) wrote:

(implied from subject: Which of these four constructs is "better", and why?)

> 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 would have to say that they are /all/ "better", in that they all have
their own unique advantages that would elevate their importance under
certain circumstances.

Your first form
struct person {
char *name;
char *surname;
is basic; it works under all circumstances.
It has the advantage of being both the simplest form (thus making it's use
as foolproof as it /can/ be, for a structure), and obvious. It permits
expansion of the structure to include recursive pointers
(i.e. { struct person *nextperson; char *name; char *surname; } )
and does not confuse the programmer with multiple identical names in
different namespaces. Additionally, it can be used (in additional
statements) to seed a typedef, if required.

It has the disadvantage of being "wordier" than the other variations, and of
exposing the details of the structure, perhaps unnecessarily.

Your second form
typedef struct {
char *name;
char *surnname;
} person;
has the advantages of data hiding, simple namespace, and simplicity of use.
However, as an anonymous structure, access to its members will always be
through a typedef'ed declaration; you will not be able to declare /this/
structure in any other way (/similar, identically populated/ structures are
possible, but the compiler will not recognize them as identical to /this/
structure; anonymous structures are always "unique"). Additionally, this
form may not be modified to include a recursive pointer
(i.e. { person *nextperson; char *name; char *surname; } )

Your third form
typedef struct person {
char *name;
char *surname;
} person;
loses the disadvantage of being an anonymous structure; you can now /also/
define variables using the simple form of the structure
(i.e. struct person *newperson
You still /cannot/ use the typedef to define a recursive pointer
(i.e. { person *nextperson; char *name; char *surname; } )
but /can/ use the structure name instead
(i.e. { struct person *nextperson; char *name; char *surname; } )

Finally, your fourth form
typedef struct _PERSON {
char *name;
char *surnmae;
} person;
you've already been told why _PERSON isn't a good name. Otherwise, this is
no different from your third form. The only additional benefit might be
that the difference in names would assist the programmer in keeping the
usages straight.

In programming, I tend to either your first form, or your third form,
depending on the circumstances.

Lew Pitcher
"In Skills, We Trust"
Reply With Quote

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. HTML 9 04-29-2013 03:35 PM
what is the advantage of using maven for java standalone app 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