coding conventions: pointers, structures, and unions

N

Neil Zanella

Hello,

Some programmers like to use a coding convention where all names of
variables that are pointers start with the letter p (and sometimes
even use similar conventions for strings and other numeric data
types). Do such programmers also have conventions for naming
structures and unions (e.g. do they like their names to
begin with s and u, so that, when the . operator is
used, it is clear whether a structure or union is
being accessed without looking at its definition)?

Thanks,

Neil
 
J

Josh Sebastian

Hello,

Some programmers like to use a coding convention where all names of
variables that are pointers start with the letter p (and sometimes
even use similar conventions for strings and other numeric data
types). Do such programmers also have conventions for naming
structures and unions (e.g. do they like their names to
begin with s and u, so that, when the . operator is
used, it is clear whether a structure or union is
being accessed without looking at its definition)?

Do a search for "Hungarian notation".

Josh
 
A

Al Bowers

Neil said:
Hello,

Some programmers like to use a coding convention where all names of
variables that are pointers start with the letter p (and sometimes
even use similar conventions for strings and other numeric data
types). Do such programmers also have conventions for naming
structures and unions (e.g. do they like their names to
begin with s and u, so that, when the . operator is
used, it is clear whether a structure or union is
being accessed without looking at its definition)?

You might be refering to the Hungarian naming convention for
identifiers.
See FAQ 17.8 at:
http://www.eskimo.com/~scs/C-faq/q17.8.html

This is primarily, but not exclusively, a C++ and Java
naming system. I have seen 'C' for the C++ Class declarations
and 'S' for struct declarations. This might be useful for
a struct typedef. For example:

typedef struct Snode
{
/* members go here */
} Snode;

and in declaring an instance of the struct:

Snode *head;

For a list of the prefixes do a web search for "Hungarian Notation".

--
Al Bowers
Tampa, Fl USA
mailto: (e-mail address removed) (remove the x)
http://www.geocities.com/abowers822/
 
F

Fronsac

Some programmers like to use a coding convention where all names of
variables that are pointers start with the letter p (and sometimes
even use similar conventions for strings and other numeric data
types).

From what you're saying, it seems you're talking about the Hungarian
notation, thought it might be some other convention I don't know of. I've
worked in a place where parameters names had to start with p. But this isn't
your case.
Do such programmers also have conventions for naming
structures and unions (e.g. do they like their names to
begin with s and u, so that, when the . operator is
used, it is clear whether a structure or union is
being accessed without looking at its definition)?

Yes. A quick search on Google with the search query "hungarian notation"
will give this link : http://web.umr.edu/~cpp/common/hungarian.html , as
well as many others. It should help you get started, but there are other
sites that cover it in more details.
 
J

Jack Klein

Hello,

Some programmers like to use a coding convention where all names of
variables that are pointers start with the letter p (and sometimes
even use similar conventions for strings and other numeric data
types). Do such programmers also have conventions for naming
structures and unions (e.g. do they like their names to
begin with s and u, so that, when the . operator is
used, it is clear whether a structure or union is
being accessed without looking at its definition)?

Thanks,

Neil

You most certainly do not need a special name for a structure or union
type when the . or -> operators are being used to know that the thing
to the left of the operator is an object of or pointer to a structure
or union type. The compiler will tell you if it is not, in a hurry.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
N

Neil Zanella

Jack Klein said:
You most certainly do not need a special name for a structure or union
type when the . or -> operators are being used to know that the thing
to the left of the operator is an object of or pointer to a structure
or union type. The compiler will tell you if it is not, in a hurry.

You are right about this particular compiler detail but from the point
of view that other users may want to understand code without looking at
header files it may help to let them know when something is a structure
from when something is a union because the syntax is the same when you
refer to members of either, so they are synactically indistinguishable
except for where they are defined.

Regards,

Neil
 
P

pete

Neil Zanella wrote:
You are right about this particular compiler detail but from the point
of view that other users may want to understand code without
looking at header files it may help to let them know when something
is a structure
from when something is a union because the syntax is the same when you
refer to members of either, so they are synactically indistinguishable
except for where they are defined.

Someone who wants to understand code without looking at header files
is neither debugging nor modifying the code in any other way.
When reading code and not debugging, I'm more interested
in the meaning of the information that a variable contains,
than I am interested in the type of the variable.
If I write code where an array of char will contain your name,
then it's going to be:
char your_name[NAME_LENGTH + 1];
If it's part of a structure that represents you,
then the name of the structure will be "you".
you.your_name
is just fine for anyone not looking at header files.
For anyone not modifying code,
I don't think that any meaningful understanding,
comes from knowing that
you.your_name
is a structure member rather than a union member.


When debugging or otherwise modifying code,
you have to look things up in header files.
 
N

Neil Zanella

pete said:
For anyone not modifying code,
I don't think that any meaningful understanding,
comes from knowing that you.your_name is a structure
member rather than a union member.

Since I am not personally a big fan of Hungarian notation, I mostly
agree with you, but suppose I have something like:

union {
int x;
char c;
} foo;

and then later on in the source code there is a line that reads:

foo.x = 1;

Later on someone decides to add the following line to the code,

foo.c = 'a';

not realizing that foo is a union and not a structure. Suddenly the
whole program misbehaves, and the compiler doesn't catch it: it's a
run time error. One possible course of action would have been in
this case to replace the union with a struct, since both fields
were needed at the same time.

I have never made the above mistake, but I've seen it happen.
Hence if Hungarian notation is being used for ALL variables
then you might as well have a rule for unions. As an aside,
Hungarian notation is not something I particularly like. It
may be OK for some pointers in some cases, but I wouldn't
say it's worthwhile using it for everything, (unless you
really have to, i.e., someone makes you use it).

Regards,

Neil
 
A

Alan Balmer

Since I am not personally a big fan of Hungarian notation, I mostly
agree with you, but suppose I have something like:

union {
int x;
char c;
} foo;

and then later on in the source code there is a line that reads:

foo.x = 1;

Later on someone decides to add the following line to the code,

foo.c = 'a';

not realizing that foo is a union and not a structure. Suddenly the
whole program misbehaves, and the compiler doesn't catch it: it's a
run time error. One possible course of action would have been in
this case to replace the union with a struct, since both fields
were needed at the same time.

Why would someone be adding an assignment statement without knowing
what kind of object he was assigning to? He might just as well have
done

d = 'c';

where d is declared double. Same kind of problem, and nothing to do
with the difference between structs and unions.
 
J

Jeffrey D. Smith

Alan Balmer said:
Why would someone be adding an assignment statement without knowing
what kind of object he was assigning to? He might just as well have
done

d = 'c';

where d is declared double. Same kind of problem, and nothing to do
with the difference between structs and unions.

That's why I use typedef for aggregates. If I need to change
a union to a struct, I just change the typedef and recompile.

typedef union _foo_
{
int x;
char c;
} Foo;

I use "Foo" everywhere, instead of "union _foo_". Changing
all instances of "Foo" to a struct is simply changing the
one typedef and recompiling.

I even use typedef for scalar types that have the potential
of changing. If I have a variable or field that is a short,
and I think there is a reasonable chance that I may need
to change it, then I'll use a typedef for its type.

typedef short Aardvark;
typedef struct _gorp_
{
Aardvark antEater;
} Gorp;

If I decide later that Aardvark needs to be something
else, like an int or long long, then it's a simple
change to the typedef and recompile.

p.s.: The so-called Hungarian notation is inherently
flawed. Never name a field or variable according to
its primitive type. Use a name that implies its purpose.


--
----------------------------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top