By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

coding conventions: pointers, structures, and unions

P: n/a

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

Nov 13 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Sun, 12 Oct 2003 23:08:30 -0230, Neil Zanella wrote:

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
Nov 13 '05 #2

P: n/a


Neil Zanella wrote:
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: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #3

P: n/a
> 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.
Nov 13 '05 #4

P: n/a
On Sun, 12 Oct 2003 23:08:30 -0230, Neil Zanella <nz******@cs.mun.ca>
wrote in comp.lang.c:

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
Nov 13 '05 #5

P: n/a
Jack Klein <ja*******@spamcop.net> wrote in message
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
Nov 13 '05 #6

P: n/a
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.

--
pete
Nov 13 '05 #7

P: n/a
pete <pf*****@mindspring.com> wrote in message
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
Nov 13 '05 #8

P: n/a
On 15 Oct 2003 16:32:32 -0700, nz******@cs.mun.ca (Neil Zanella)
wrote:
pete <pf*****@mindspring.com> wrote in message
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.


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.

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


--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 13 '05 #9

P: n/a
"Alan Balmer" <al******@att.net> wrote in message
news:0i********************************@4ax.com...
On 15 Oct 2003 16:32:32 -0700, nz******@cs.mun.ca (Neil Zanella)
wrote:
pete <pf*****@mindspring.com> wrote in message
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.


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.

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


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!
Nov 13 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.