472,982 Members | 2,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Naming convention for pointer-to-foo types

Something I've been puzzled over for a while. This really has no consequence at
all, but I'm curious for the _reason_ behind where we commonly place the
asterisk when declaring pointer variables.

When defining a variable which is of pointer-to-foo type, it seems the
'convention' is to put the asterisk before the variable's identifier, like this:
foo *ptr;
The C standards all seem to use this conventions, but it is equally valid
(AFAIK) to place the asterisk after the variable's type, like this:
foo* ptr;
....or even on its own:
foo * ptr;
Now, am I correct in suggesting that the 'pointerness' of a variable is an
attribute of its type, rather than its identifier? After all, when specifying a
function's arguments in a declaration, we declare the argument's expected type
as pointer-to-foo:
int myFunction(foo *, foo *); /* common */
or int myFunction(foo*, foo*); /* not so common */
The qualified type of the variable is pointer-to-foo. No less than that.
As such, why do we specify the 'pointerness' of a variable by putting the
asterisk by its name, when _really_ it is an attribute of its type? To me it
seems much more logical to put the asterisk with the rest of the type, but maybe
I just think crooked.

So far I have seen only one instance of using the 'conventional' manner which is
more logical than the mentioned alternative, and that concerns pointers to
constant foos (and not mistaking them for constant pointers to foos):
foo *const ptr;
seems to be less ambiguous than
foo* const ptr;
Vice-versa however, it seems the alternative method wins:
const foo* ptr;
makes more sense to me than
const foo *ptr;

I guess though when you've been reading C for more than thirty years and you
have dreams where everyone speaks C, nothing is ambiguous. Just wanting some
opinions.

Koster

Nov 13 '05 #1
9 4902
Koster wrote:
Something I've been puzzled over for a while. This really has no consequence at
all, but I'm curious for the _reason_ behind where we commonly place the
asterisk when declaring pointer variables.
....
I guess though when you've been reading C for more than thirty years and you
have dreams where everyone speaks C, nothing is ambiguous. Just wanting some
opinions.


My opinion is that type* foo; makes the most sense and it's the language
that doesn't make sense because type* foo, bar; doesn't mean what it looks
like it means, so I usually write type *foo; unless I'm writing C++, then
I write type* foo; because that's how everyone else seems to do it and
if you don't use commas it hides the senselessness of it all and since
there's less need to use commas in C++ declarations it makes more sense
there than it does in C. *gasp*

Matt Gregory

Nov 13 '05 #2
In message <m0********************************@4ax.com>
Koster <ko***********@yahoo.com.sg> wrote:
When defining a variable which is of pointer-to-foo type, it seems the
'convention' is to put the asterisk before the variable's identifier, like
this:
foo *ptr;
The C standards all seem to use this conventions, but it is equally valid
(AFAIK) to place the asterisk after the variable's type, like this:
foo* ptr;
...or even on its own:
foo * ptr;
Now, am I correct in suggesting that the 'pointerness' of a variable is an
attribute of its type, rather than its identifier?
Yes.
As such, why do we specify the 'pointerness' of a variable by putting the
asterisk by its name, when _really_ it is an attribute of its type? To me
it seems much more logical to put the asterisk with the rest of the type,
but maybe I just think crooked.


The reason is that C declarations are designed to reflect the use of the
identifier. The declaration starts with a basic type, and then declares how
you would get from the identifier to that type using *, [] and ().

Thus, when you say

foo *ptr;

you are saying that "*ptr" is of type foo.

Likewise

foo func(int);

says that the expression "func(2)" is of type foo, and

foo array[10];

says that "array[n]" is of type foo. You can carry on in this vein to
construct monstrosities like

foo *(*x[10])(void);

which have to be deciphered by understanding that the expression
"*(*x[n])()" will have type foo, and working backwards from there to the
type of x (an array of 10 pointers to functions taking void and returning a
pointer to int). The example program cdecl (IIRC) in K&R will deconstruct
declarations like this.

But the really clinching argument in my view for putting the * next to the
identifier is to prevent the potential confusion arising from:

foo* fred, jim;

That makes it look as though jim will have type foo*. Writing it the
conventional way

foo *fred, jim;

can still cause potential confusion, so should be used with care, but at
least the formatting accurately reflects the meaning.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 13 '05 #3
Koster <ko***********@yahoo.com.sg> wrote:
Something I've been puzzled over for a while. This really has no consequence at
all, but I'm curious for the _reason_ behind where we commonly place the
asterisk when declaring pointer variables.

When defining a variable which is of pointer-to-foo type, it seems the
'convention' is to put the asterisk before the variable's identifier, like this:
foo *ptr;
The C standards all seem to use this conventions, but it is equally valid
(AFAIK) to place the asterisk after the variable's type, like this:
foo* ptr;
...or even on its own:
foo * ptr; Yes, all three declarations are equivalent.
Now, am I correct in suggesting that the 'pointerness' of a variable is an
attribute of its type, rather than its identifier? After all, when specifying a
function's arguments in a declaration, we declare the argument's expected type
as pointer-to-foo:
int myFunction(foo *, foo *); /* common */
or int myFunction(foo*, foo*); /* not so common */
The qualified type of the variable is pointer-to-foo. No less than that.
As such, why do we specify the 'pointerness' of a variable by putting the
asterisk by its name, when _really_ it is an attribute of its type? To me it
seems much more logical to put the asterisk with the rest of the type, but maybe
I just think crooked.
It's a matter of style and readability. Consider:

foo *ptr, bar;
foo * ptr, bar;
foo* ptr, bar;

In all three cases you declare a variable of type pointer-to-foo (ptr)
and a variable of type foo (bar). Which version would you prefer to
see in actual code?


So far I have seen only one instance of using the 'conventional' manner which is
more logical than the mentioned alternative, and that concerns pointers to
constant foos (and not mistaking them for constant pointers to foos):
foo *const ptr;
seems to be less ambiguous than
foo* const ptr;
Vice-versa however, it seems the alternative method wins:
const foo* ptr;
makes more sense to me than
const foo *ptr;
Again, what if you see: const foo* ptr, bar;
I guess though when you've been reading C for more than thirty years and you
have dreams where everyone speaks C, nothing is ambiguous.

Maybe I've got a chance to really get a grip on C, then? Only fifteen
odd years to go, yippihh-yeah! ;-)

<SNIP>

Regards

Irrwahn
--
What does this red button do?
Nov 13 '05 #4
Kevin Bracey <ke**********@tematic.com> wrote:

<SNIP>
Likewise

foo func(int);

says that the expression "func(2)" is of type foo, and

foo array[10];

says that "array[n]" is of type foo. You can carry on in this vein to
construct monstrosities like

foo *(*x[10])(void);

Got one for you:

char *(*(*foo[5])())();

Like it? ;o)

Irrwahn
--
What does this red button do?
Nov 13 '05 #5

"Irrwahn Grausewitz" <ir*****@freenet.de> wrote in message
It's a matter of style and readability. Consider:

foo *ptr, bar;
foo * ptr, bar;
foo* ptr, bar;

In all three cases you declare a variable of type pointer-to-foo (ptr)
and a variable of type foo (bar). Which version would you prefer to
see in actual code?

Obviously number one. However why only three options?

foo *ptr; /* comment */
foo bar; /* bar holds beer pumps */

would be a better style. If the function uses so many variables that you
need to cram more than one to a line, break it up.
Nov 13 '05 #6
Koster <ko***********@yahoo.com.sg> wrote:
Something I've been puzzled over for a while. This really has no consequence at
all, but I'm curious for the _reason_ behind where we commonly place the
asterisk when declaring pointer variables.

When defining a variable which is of pointer-to-foo type, it seems the
'convention' is to put the asterisk before the variable's identifier, like this:
foo *ptr;
The C standards all seem to use this conventions, but it is equally valid
(AFAIK) to place the asterisk after the variable's type, like this:
foo* ptr;
...or even on its own:
foo * ptr;
Now, am I correct in suggesting that the 'pointerness' of a variable is an
attribute of its type, rather than its identifier?


In C, declarations aren't:

<type> <identifier>

instead, they are

<base type> <"expression" involving identifier>

In the case of

foo *ptr;

the base type is "foo" and the "expression involving identifier" is
"*ptr". This is supposed to be read as "*ptr has type foo".

After all, you don't write

foo[5] array;

to try and declare an array of 5 foos, do you? :)

- Kevin.

Nov 13 '05 #7
Koster <ko***********@yahoo.com.sg> wrote in message news:<m0********************************@4ax.com>. ..
Something I've been puzzled over for a while. This really has no consequence
at all, but I'm curious for the _reason_ behind where we commonly place the
asterisk when declaring pointer variables.

[snip rest]

I'm sure the C declarator syntax seemed like a good idea at the time
(declaration mimics use), but it always causes heartburn when trying
to talk about types.

The short answer as to why most people type "int *foo;" instead of
"int* foo;" or "int * foo;" is that the declarator syntax ties the
"pointerness" of a variable to the identifier, not the type specifier.
IOW, if you type

int* foo, bar;

the compiler interprets it as the *declarators* "*foo" and "bar"
having type int. It doesn't matter how much whitespace comes between
the type specifier, the asterisk, and the identifier, the asterisk is
still associated with the identifier. The type of the identifier
"foo" is pointer-to-int, but the syntax of the declaration requires
that we state it as the declarator "*foo" has type int. Therefore it
just makes more sense to write it as "int *foo;" since that reflects
how it's interpreted.

If you look up the grammar for the above declaration, it basically
breaks down like this (I'm not showing all the possible productions
for each nonterminal, just as it applies to the above declaration):

DECLARATION
DECLARATION_SPECIFIERS
TYPE_SPECIFIER
INTEGER_TYPE_SPECIFIER
SIGNED_TYPE_SPECIFIER
int
INITIALIZED_DECLARATOR_LIST
INITIALIZED_DECLARATOR_LIST
INITIALIZED_DECLARATOR
DECLARATOR
POINTER_DECLARATOR
POINTER
*
DIRECT_DECLARATOR
SIMPLE_DECLARATOR
IDENTIFIER
foo
,
INITIALIZED_DECLARATOR
DECLARATOR
DIRECT_DECLARATOR
SIMPLE_DECLARATOR
IDENTIFIER
bar
;

I think that's right. Anyway, as you can see, the pointer part of the
declaration is tied to the declarator for foo, not the type specifier.
Nov 13 '05 #8
bd
Koster wrote:
Something I've been puzzled over for a while. This really has no
consequence at all, but I'm curious for the _reason_ behind where we
commonly place the asterisk when declaring pointer variables. [snip] So far I have seen only one instance of using the 'conventional' manner
which is more logical than the mentioned alternative, and that concerns
pointers to constant foos (and not mistaking them for constant pointers to
foos): foo *const ptr;
seems to be less ambiguous than
foo* const ptr;
Vice-versa however, it seems the alternative method wins:
const foo* ptr;
makes more sense to me than
const foo *ptr;
Until you have:
const foo* ptr, nonptr;

I'm not sure what foo const *x would do, but cdecl dosen't like it.
I guess though when you've been reading C for more than thirty years and
you
have dreams where everyone speaks C, nothing is ambiguous. Just wanting
some opinions.

Koster


--
Please come home with me ... I have Tylenol!!

Nov 13 '05 #9
bd <bd*****@users.sf.net> wrote:
[...]
I'm not sure what foo const *x would do, but cdecl dosen't like it.


foo const *x;

is identical in effect to:

const foo *x;

(and in fact the former is more logically consistent)

- Kevin.

Nov 13 '05 #10

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

Similar topics

7
by: Nobody | last post by:
sorry if this is OT, not sure if it is since it is a C++ q, sort of :). I am working on a C++ library where all the files use the 8.3 convention. I am now adding some collection classes. The...
7
by: DEX | last post by:
Main page of my NC: http://www.ddmrm.com/coding/cpp/naming/cpp.naming.main.html Rules of my NC: http://www.ddmrm.com/coding/cpp/naming/cpp.naming.rules.html Comments are welcome. ...
5
by: Ook | last post by:
Is there any kind of naming convention for accessor and modifiers? What I've been doing is something like this: // accessor int getSize(); // Modifier void setSize( int newsize); private:
2
by: Joey Lee | last post by:
Hi, Microsoft provided some naming conventions for namespace, classes, properties and etc. However I am looking for naming convention for keys in resource files for multiple languages. Any...
11
by: tonicvodka | last post by:
What is the naming convention for functions in c#?
5
by: rastaman | last post by:
Hi all, I know of the existence of Object Naming Conventions for Visual Basic 6. Now I'm reading some books about VB .NET, but the names used for the objects are button1, picturebox1, etc. I...
0
by: Stan Y | last post by:
An example of such convention is http://www.devx.com/dbzone/Article/10866/1954?pf=true. But what is the most prevalent naming convention in the relation database field? Thanks, Stan
3
by: Sam | last post by:
Hi all What's standard naming convention for module and global variables in .net? Do we still use lower case prefix "m" for module (or class) scope and "g" for global scope? Regards, Sam
0
by: paul.hester | last post by:
Hi all, I can't find a consistent naming convention rule for form elements and/or server controls with ASP .NET 2.0. What conventions do you generally use? For example, for a user name text...
114
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.