473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Variable and typename naming

In C, if I want similar names for typename and variable,
is there any good way you can recommend me to do that?

For example, I have a struct containing a region, which
I have named it, appropriately, struct region, now when I
declare variables, I want to have the same, or a very
similar name, since I can't come up with another good name.

Now, because the different namespaces, I could declare it
struct region region.
But maybe that is obscure and bad coding practice?
Or maybe put a captial letter in the typename, like
struct Region region, or even a
typedef struct region { ... } Region. I don't know,
so I'm grateful for answers.

Thanks!
Apr 11 '06 #1
13 1823
In article <ob*******************@newsb.telia.net>,
Edward Gregor <ed****@hotmail.com> wrote:
In C, if I want similar names for typename and variable,
is there any good way you can recommend me to do that? For example, I have a struct containing a region, which
I have named it, appropriately, struct region, now when I
declare variables, I want to have the same, or a very
similar name, since I can't come up with another good name. Now, because the different namespaces, I could declare it
struct region region.
But maybe that is obscure and bad coding practice?
Yeah.
Or maybe put a captial letter in the typename, like
struct Region region, or even a
typedef struct region { ... } Region. I don't know,
so I'm grateful for answers.


It isn't uncommon to use slightly different forms for different
logical layers. For example, some people might use

struct region_s { ... };
typedef struct region_s region_t;
region_t region;

There are different naming systems in use, especially when
it comes to procedures. For example, some people
runthenamestogether, some people CapitalizeEachWord,
some people use_underscores_between_words, some name
procedures with a suffix combination that indicates the types
it operates on; and some people impose the requirement that
a procedure name must either be a verb (e.g., die()) or else
must be a clause indicating a verb and a (possibly qualified) noun
(e.g., TotalSalaries, print_small_cheques). [People who do that
often also have particular format requirements for object identifiers.]

--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Apr 11 '06 #2
Walter Roberson wrote:
struct region_s { ... };
typedef struct region_s region_t;
region_t region;


What would the _s and _t suffices stand for in this example?

Thanks for the help!
Apr 11 '06 #3
Edward Gregor <ed****@hotmail.com> writes:
Walter Roberson wrote:
struct region_s { ... };
typedef struct region_s region_t;
region_t region;


What would the _s and _t suffices stand for in this example?


struct and type or typedef, respectively.

(Personally, I wouldn't bother with the typedef. "struct region" is a
perfectly good name for the type; there's little advantage in
inventing another name for it.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 12 '06 #4
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
Edward Gregor <ed****@hotmail.com> writes:
Walter Roberson wrote:
struct region_s { ... };
typedef struct region_s region_t;
region_t region;
What would the _s and _t suffices stand for in this example?
struct and type or typedef, respectively. (Personally, I wouldn't bother with the typedef. "struct region" is a
perfectly good name for the type; there's little advantage in
inventing another name for it.)


I know some people have posted along those lines in the past. I
can't say as I agree with that viewpoint, though.

A typedef can be viewed as an abstract type name: once something
has been typedef'd, one can use the type name in user code without
any consideration of what is "underneath the hood".

"struct region" on the other hand requires that one knows something
about what is under the hood -- namely that it is a struct. At
the very least, that tells you some things that the implementation
-cannot- do with it, such as arithmetic operations. That level of
knowledge is none of the business of the user code that just needs
the name of an abstract data type.

One could, I suppose, go ahead and declare that -all- abstract data
types names in a program shall be struct tags, so as to put everything
on an equal footing. It seems to me, though, that doing so would
merely increase the amount of typing, and would make it more of
a nuisance at the implementation level, since one would have to
put in a tag reference even if the underpinings turned out to be
an arithmetic type. typedef'ing a struct tag into type name
does not have those disadvantages.

But then, what do I know? My courses were way back in the days when
Aho, Hopcroft and Ullman were cutting edge.
--
Programming is what happens while you're busy making other plans.
Apr 12 '06 #5
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
Edward Gregor <ed****@hotmail.com> writes:
Walter Roberson wrote:
struct region_s { ... };
typedef struct region_s region_t;
region_t region; What would the _s and _t suffices stand for in this example?

struct and type or typedef, respectively.

(Personally, I wouldn't bother with the typedef. "struct region" is a
perfectly good name for the type; there's little advantage in
inventing another name for it.)


I know some people have posted along those lines in the past. I
can't say as I agree with that viewpoint, though.

A typedef can be viewed as an abstract type name: once something
has been typedef'd, one can use the type name in user code without
any consideration of what is "underneath the hood".

"struct region" on the other hand requires that one knows something
about what is under the hood -- namely that it is a struct. At
the very least, that tells you some things that the implementation
-cannot- do with it, such as arithmetic operations. That level of
knowledge is none of the business of the user code that just needs
the name of an abstract data type.

[snip]

Yes, If you want an abstract data type, a typedef is just the thing.
(FILE in <stdio.h> is a good example of this.)

If the code using the type needs to know that it's a struct (e.g., if
it refers to its members), a typedef isn't particularly helpful.

(Possibly a lot of things *should* be abstract types that aren't.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 12 '06 #6
Edward Gregor wrote:
In C, if I want similar names for typename and variable,
is there any good way you can recommend me to do that?

For example, I have a struct containing a region, which
I have named it, appropriately, struct region, now when I
declare variables, I want to have the same, or a very
similar name, since I can't come up with another good name.
What's wrong with `r'?
Now, because the different namespaces, I could declare it
struct region region.
But maybe that is obscure and bad coding practice?
Or maybe put a captial letter in the typename, like
struct Region region, or even a
typedef struct region { ... } Region. I don't know,
so I'm grateful for answers.


I would do

typedef struct region Region;
struct region
{
...
};
August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
Apr 12 '06 #7

Walter Roberson wrote:
In article <ob*******************@newsb.telia.net>,
Edward Gregor <ed****@hotmail.com> wrote:
In C, if I want similar names for typename and variable,
is there any good way you can recommend me to do that?
It isn't uncommon to use slightly different forms for different
logical layers. For example, some people might use

struct region_s { ... };
typedef struct region_s region_t;
region_t region;

From the info page for libc:

"Some additional classes of identifier names are reserved for future
extensions to the C language or the POSIX.1 environment. While using
these names for your own purposes right now might not cause a problem,
they do raise the possibility of conflict with future versions of the C
or POSIX standards, so you should avoid these names.
[snip]
* Names that end with `_t' are reserved for additional type names."

Does this state that you should not roll your own type def with a
suffix
"_t", or by "additional type names" is it in fact referring to your own
additional type names and recommending "_t" as a suffix?

Is that info page relevant to the standard? (I'm getting it from a
stock Fedora installation.)

Apr 12 '06 #8
>In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
(Personally, I wouldn't bother with the typedef. "struct region" is a
perfectly good name for the type; there's little advantage in
inventing another name for it.)

In article <e1**********@canopus.cc.umanitoba.ca>
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:I know some people have posted along those lines in the past. I
can't say as I agree with that viewpoint, though.

A typedef can be viewed as an abstract type name: once something
has been typedef'd, one can use the type name in user code without
any consideration of what is "underneath the hood".
But so can a "struct". After all, "struct" stands for "STRange
spelling for User-defined abstraCt Type", as you can see from the
uppercased letters in the phrase. :-)

Note that "struct <identifier> {" actually *does* define a new
type (typedef does not -- in the sequence "typedef struct <id> {"
it is the "struct ... {" part that defines the type). This new
type is different from, and incompatible with, all other
differently-named types:

struct time { double val; };
struct temperature { double val; };
struct money { double val; };

gives you three incompatible types, so that you cannot assign a
"temperature" to a "time", for instance.

In addition, structs allow (but do not require) you to make the
type completely opaque, by omitting the "{ <contents> }" part:

struct schedule;

Now "schedule" is an opaque type; none of its members can be
modified. This enforces the abstract-ness: not only *should*
you not inspect the innards, you *can*not.

The one big flaw in this technique occurs in C89, where it is
impossible to create constants that have a structure type. This
is fixed in C99, with its "compound literals".
"struct region" on the other hand requires that one knows something
about what is under the hood -- namely that it is a struct. At
the very least, that tells you some things that the implementation
-cannot- do with it, such as arithmetic operations. That level of
knowledge is none of the business of the user code that just needs
the name of an abstract data type.

One could, I suppose, go ahead and declare that -all- abstract data
types names in a program shall be struct tags, so as to put everything
on an equal footing.
Yes; and I tend to argue for this, except where the convenience of
exposing the hidden mechanisms underlying the type (so that it is
*not* an "abstract" type after all) is sufficiently compelling.
If the user is allowed to "know" that the type is integral,
floating-point, pointer-y, or similar, and make use of that knowledge
(e.g., by incrementing variables of that type), then -- and only
then -- does it really become a candidate for "typedef"ing.
It seems to me, though, that doing so would
merely increase the amount of typing, and would make it more of
a nuisance at the implementation level, since one would have to
put in a tag reference even if the underpinings turned out to be
an arithmetic type. typedef'ing a struct tag into type name
does not have those disadvantages.


If you write, e.g.:

temperature_type x, y, z;
...
z = x + y;

you have demonstrated that "temperatures" are not abstract after
all and can simply be summed (which is not generally true of
temperatures, except if expressed in Kelvin or Rankine or similar).
So I would say, instead, "using a struct tag does not have that
disadvantage". :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Apr 12 '06 #9
Bill Pursell wrote:

Walter Roberson wrote:
In article <ob*******************@newsb.telia.net>,
Edward Gregor <ed****@hotmail.com> wrote:
In C, if I want similar names for typename and variable,
is there any good way you can recommend me to do that?


It isn't uncommon to use slightly different forms for different
logical layers. For example, some people might use

struct region_s { ... };
typedef struct region_s region_t;
region_t region;

From the info page for libc:

"Some additional classes of identifier names are reserved for future
extensions to the C language or the POSIX.1 environment. While using
these names for your own purposes right now might not cause a problem,
they do raise the possibility of conflict with future versions of the C
or POSIX standards, so you should avoid these names.
[snip]
* Names that end with `_t' are reserved for additional type names."

Does this state that you should not roll your own type def with a
suffix
"_t", or by "additional type names" is it in fact referring to your own
additional type names and recommending "_t" as a suffix?

Is that info page relevant to the standard? (I'm getting it from a
stock Fedora installation.)


I have enough respect for POSIX
to use a more spelled out "_type" suffix instead,
as in e_type or d_type, even though I don't anticipate
writing any code for POSIX in the forseeable future.

--
pete
Apr 12 '06 #10
On Tue, 11 Apr 2006 22:36:04 UTC, Edward Gregor <ed****@hotmail.com>
wrote:
In C, if I want similar names for typename and variable,
is there any good way you can recommend me to do that?

For example, I have a struct containing a region, which
I have named it, appropriately, struct region, now when I
declare variables, I want to have the same, or a very
similar name, since I can't come up with another good name.

Now, because the different namespaces, I could declare it
struct region region.
But maybe that is obscure and bad coding practice?
Or maybe put a captial letter in the typename, like
struct Region region, or even a
typedef struct region { ... } Region. I don't know,
so I'm grateful for answers.


I'm trained in to use only capital letters for macro names and self
defined types.
--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Apr 12 '06 #11
Chris Torek wrote:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:

<snip>

In addition, structs allow (but do not require) you to make the
type completely opaque, by omitting the "{ <contents> }" part:

struct schedule;

Now "schedule" is an opaque type; none of its members can be
modified. This enforces the abstract-ness: not only *should*
you not inspect the innards, you *can*not.


<snip>

In APIs such as those provided for Windows, opaque types were at one time
*not* defined as /empty structs/ - in the end-user's header files - but
rather as unsigned longs, e.g., a HANDLE type was typically defined as:
typedef unsigned long HANDLE;

Now, when some OS API put some value into one of these HANDLEs the
programmer had no idea as to its /meaning/, i.e., it could have been an
index into some internal OS table, a pointer to a struct, some just a
number, ... whatever: being /just/ a long gave very little clue as to its
use/implmentation.

However, nowadays, I think, MS exposes most of its HANDLEs like this:

#define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct
name##__ *name

Laying that out a little clearer [yes I know about the excess spaces in the
macro!]

#define DECLARE_HANDLE(name) \
\
struct name##__ \
{ \
int unused; \
\
}; \
\
typedef struct name##__ * name

So, nowadays they/you use this like this:

DECLARE_HANDLE(HANDLE);

HANDLE hThingmy;

Two questions from this then.

1. Although a Windows' API can still return any of the list above [an index
into some internal OS table, a pointer to a struct, some just a number, ...]
in the /guise/ of *this* type of HANDLE, it does seem to me that it
encourage the consumer to at least /initially think/ of some handle type as
[probably] some sort of struct pointer - and thus, it /perhaps/ encourages
the same person to explore [hack] these - so, why drop the simple /disguise/
of using an unsigned long [or some other /plain/ type capable of holding as
pointer] in favour of an opaque struct?

2. Why does MS use an int unused member when they could simply leave the
struct empty - compatibility of some sort?

--
==============
*Not a pedant*
==============
Apr 17 '06 #12
>Chris Torek wrote:
In addition, structs allow (but do not require) you to make the
type completely opaque, by omitting the "{ <contents> }" part:
struct schedule;
Now "schedule" is an opaque type; none of its members can be
modified. This enforces the abstract-ness: not only *should*
you not inspect the innards, you *can*not.

In article <e1**********@news.ox.ac.uk> pemo <us***********@gmail.com> wrote:In APIs such as those provided for Windows, opaque types were at one time
*not* defined as /empty structs/ - in the end-user's header files - but
rather as unsigned longs, e.g., a HANDLE type was typically defined as:
typedef unsigned long HANDLE;

Now, when some OS API put some value into one of these HANDLEs the
programmer had no idea as to its /meaning/, i.e., it could have been an
index into some internal OS table, a pointer to a struct, some just a
number, ... whatever: being /just/ a long gave very little clue as to its
use/implmentation.

However, nowadays, I think, MS exposes most of its HANDLEs like this: [edited slightly]#define DECLARE_HANDLE(name) struct name##__ { int unused; }; \
typedef struct name##__ *name ...So, nowadays they/you use this like this:

DECLARE_HANDLE(HANDLE);

HANDLE hThingmy;

Two questions from this then.
I have a third one: why bother appending the double underscore? :-)
1. [snippage ...] why drop the simple /disguise/
of using an unsigned long [or some other /plain/ type capable of holding as
pointer] in favour of an opaque struct?
I cannot answer for them, but there is no guarantee that there is
any appropriate integer type (even C99 does not require that there
*be* an "intptr_t"), and if there is, why do a lot of work to
dig it out when you can just use "struct opaque *"?
2. Why does MS use an int unused member when they could simply leave the
struct empty - compatibility of some sort?


This one is easy enough: an empty struct -- struct foo {} -- is not
syntactically valid. If you meant "why not just leave the type
incomplete", that I cannot answer.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Apr 30 '06 #13
On Mon, 17 Apr 2006 12:11:20 +0100, "pemo" <us***********@gmail.com>
wrote:
<snip>
However, nowadays, I think, MS exposes most of its HANDLEs like this:

#define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct
name##__ *name So, nowadays they/you use this like this:

DECLARE_HANDLE(HANDLE);

HANDLE hThingmy;

Two questions from this then.

1. Although a Windows' API can still return any of the list above [an index
into some internal OS table, a pointer to a struct, some just a number, ...]
in the /guise/ of *this* type of HANDLE, it does seem to me that it
encourage the consumer to at least /initially think/ of some handle type as
[probably] some sort of struct pointer - and thus, it /perhaps/ encourages
the same person to explore [hack] these - so, why drop the simple /disguise/
of using an unsigned long [or some other /plain/ type capable of holding as
pointer] in favour of an opaque struct?
Perhaps because struct uniquetag ... is a new type in C, whereas
typedef anything is not. This allows/requires the compiler to detect
if you try to misuse one type of handle (pointer) to a different one.
2. Why does MS use an int unused member when they could simply leave the
struct empty - compatibility of some sort?


Standard C doesn't allow a struct (or union) with no members. <OT>C++
does, and has sensible uses for struct/class with no explicit members
and/or no data members at all.</> They don't need or want
Windows-specific declarations portable to other systems, but might
want them to work on other Windows-targetable compiler(s) like GCC.

- David.Thompson1 at worldnet.att.net
May 1 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: santosh | last post by:
Hello, I have a doubt about static variable. class B { private: static int x; public: static set(int y) {
10
by: Clay_Culver | last post by:
I have heard that it is possible to use classes + template magic to make standalone functions (or maybe just a functor which acts like a function) which can accept variable length arguments without...
4
by: VM | last post by:
What's the correct variable naming in C#? How should a form be called (eg. Frm_myForm)? or an int variable (eg. Int_Var) or a DataGrig (eg. DG_myGrid)? I'm writing an application but I would like...
6
by: Michael | last post by:
Hi, What is the naming convention in .NET when it comes to naming a variable like "StudentID"? Is it "StudentID" or "StudentId"? For variable like "myHtml", I know the "HTML" part must be...
3
by: Jess | last post by:
Is there a way to print a variable's data type - like int, string, dbl?
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
17
by: Control Freq | last post by:
Hi, Not sure if this is the right NG for this, but, is there a convention for the variable names of a Session variable? I am using .NET 2.0 in C#. I am new to all this .NET stuff, So, any...
13
by: Justcallmedrago | last post by:
How would you declare and assign a variable inside a function THAT HAS THE NAME OF A PARAMETER YOU PASSED example: when you call createvariable("myvariable") it will declare the variable...
4
by: mthread | last post by:
Hi, I am using a string variable in which I do lot of appending. The only difficulty I am facing as of now is appending a integer/float value to this variable. Although I can accomplish this task...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.