473,468 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

coding conventions: pointers, structures, and unions


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
9 3078
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


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
> 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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

94
by: Gabriel Zachmann | last post by:
Is it correct to say that strong/weak typing does not make a difference if one does not use any pointers (or adress-taking operator)? More concretely, I am thinking particularly of Python vs C++....
4
by: Konstantin Shemyak | last post by:
I have a big structure tree. All leaves are scalar values (no pointers). Present are arrays, structures and unions. I want to be able to store/read the content of the structure in/from a file, and...
13
by: brian | last post by:
Quick question: if I have a structure: struct foo { unsigned char *packet; unsigned char *ip_src; };
47
by: sunglo | last post by:
Some time a go, in a discussion here in comp.lang.c, I learnt that it's better not to use a (sometype **) where a (void **) is expected (using a cast). Part of the discussion boiled down to the...
11
by: junky_fellow | last post by:
Do all double pointers have same size on a particular implementation. eg char **c_ptr; int **i_ptr; Is the size of c_ptr and i_ptr always same (on same implementation) or it may be different...
7
by: Ralph Lund | last post by:
Hi. I am starting a new project with C#. I am searching for "good" coding conventions. I know that there are some coding conventions from microsoft, (but they are very extensive and not clear)....
50
by: Konrad Palczynski | last post by:
I am looking for tool to validate conformity to defined coding standard. I have already found Parasoft's C++ Test, but it is quite expensive. Is there any Open Source alternative? I do not need...
19
by: auratius | last post by:
http://www.auratius.co.za/CSharpCodingStandards.html Complete CSharp Coding Standards 1. Naming Conventions and Styles 2. Coding Practices 3. Project Settings and Project Structure 4....
6
by: Ravikiran | last post by:
Hi, I want know the differences between Unions and Structures in C programming. Thank you..
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.