Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is it just a matter of taste?

1 view
Skip to first unread message

Dan Chicot

unread,
Apr 26, 2000, 3:00:00 AM4/26/00
to
All the text books that I use state that a pointer should be declared
something like this: -

char *aPointer=NULL; /*for example*/

Does it not make more sense to declare them thus: -

char* aPointer=NULL;

The former seems to read "I'm declaring a variable of type char, well
actually it's not, it's a pointer to a char, and I'm calling it
'aPointer' ", whereas the latter says "I'm declaring a 'pointer to a
char' and it's name is 'aPointer' "
Is there a good reason why the second approach is best avoided?

Mark McIntyre

unread,
Apr 26, 2000, 3:00:00 AM4/26/00
to
On Wed, 26 Apr 2000 22:39:14 GMT, chi...@btinternet.com (Dan Chicot)
wrote:

>All the text books that I use state that a pointer should be declared
>something like this: -
>
>char *aPointer=NULL; /*for example*/

Original C style. Perfectly legit. Also makes more sense in this sort
of context, since the * is "bound" to what it makes into a pointer.
char *a, *b; // a and b are both pointers to char

>Does it not make more sense to declare them thus: -
>
>char* aPointer=NULL;

So called C++ style, introduced by C++ and accepted also by C. There
is absolutely no difference, except that you can't do what I did above
quite as obviously since the * is "bound" to the type..

char* a, b; // b is a char not a pointer to char

char* a, *b; // mixing styles can confuse novices...

>The former seems to read "I'm declaring a variable of type char, well
>actually it's not, it's a pointer to a char, and I'm calling it
>'aPointer' ", whereas the latter says "I'm declaring a 'pointer to a
>char' and it's name is 'aPointer' "

They read identically. You read right to left in both C and C++, and
whitespace is irrelevant.

"char* aPointer" = "variable aPointer is a pointer to char".
"char *aPointer = "variable aPointer is a pointer to char".
"char * aPointer" = "variable aPointer is a pointer to char".

>Is there a good reason why the second approach is best avoided?

Provided you only declare one object per line, no, either is
completely fine. Its purely personal preference.
Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Ben Pfaff

unread,
Apr 30, 2000, 3:00:00 AM4/30/00
to
chi...@btinternet.com (Dan Chicot) writes:

> All the text books that I use state that a pointer should be declared
> something like this: -
>
> char *aPointer=NULL; /*for example*/
>

> Does it not make more sense to declare them thus: -
>
> char* aPointer=NULL;

No. Please consider the following declaration:

char* a, b;

What has been declared? Given that, what can you conclude about
which part of the declaration `owns' the *: is it `char' or `a'?

Allin Cottrell

unread,
May 1, 2000, 3:00:00 AM5/1/00
to
Dan Chicot wrote:
>
> All the text books that I use state that a pointer should be declared
> something like this: -
>
> char *aPointer=NULL; /*for example*/
>
> [This] seems to read "I'm declaring a variable of type char, well

> actually it's not, it's a pointer to a char, and I'm calling it
> 'aPointer'...

You could read it this way (since "*" means "contents of"): "I'm
declaring a variable of type char, namely, that to which
'aPointer' points."

--
Allin Cottrell
Department of Economics
Wake Forest University, NC

Kaz Kylheku

unread,
May 1, 2000, 3:00:00 AM5/1/00
to
On Wed, 26 Apr 2000 22:39:14 GMT, Dan Chicot <chi...@btinternet.com> wrote:
>All the text books that I use state that a pointer should be declared
>something like this: -
>
>char *aPointer=NULL; /*for example*/
>
>Does it not make more sense to declare them thus: -
>
>char* aPointer=NULL;

It is not incorrect do declare this way and even some good programmers do it
(e.g. Bjarne Stroustrup is undoubtedly a great programmer and he sometimes
writes example declarations the latter way in his books but he doesn't
do it in ways where it appears ambiguous.)

>The former seems to read "I'm declaring a variable of type char, well


>actually it's not, it's a pointer to a char, and I'm calling it

>'aPointer' ", whereas the latter says "I'm declaring a 'pointer to a
>char' and it's name is 'aPointer' "

>Is there a good reason why the second approach is best avoided?

Yes. The second approach ignores the grammar of the language, by putting
the major whitespace division in a place other than the major
grammatical division.

Would you write a polynomial expression this way?

x * x+2 * x+1

Hell no, you would write:

x*x + 2*x + 1

right?

The former is confusing because multiplication has higher precedence,
but the whitespace separation is written in a way which suggests that addition
has higher precedence.

The major constituents of a declaration are the declaration specifier list,
and the list of declarators: for example, in the declaration

const unsigned int volatile *p, q[3];

the major division is between the ``const unsigned int volatile'', which
is called the declaration specifier list, and the ``*p, q[3]'' which is
the declarator list.

(In anticipation of the usual c.l.c hecklers, I have to add that the top level
division is actually between the semicolon and the rest of the declaration,
but allow not punctuation to distract from the point.)

Thus it would be misleading and confusing to write the declaration
as:

const unsigned int volatile * p, q[3];

In fact I would wonder whether the programmer understands the declaration
syntax and all and would probably pay unduly close attention to the code as a
result. Lack of clarity wastes everyone's time.

--
#exclude <windows.h>

Mark McIntyre

unread,
May 1, 2000, 3:00:00 AM5/1/00
to
Dan Chicot wrote:
>
> All the text books that I use state that a pointer should be declared
> something like this: -
>
> char *aPointer=NULL; /*for example*/
>
> [This] seems to read "I'm declaring a variable of type char, well

> actually it's not, it's a pointer to a char, and I'm calling it
> 'aPointer'...

you're reading it from the wrong end. You read right-to-left in
declarations, and you ignore any definition that arises
simultaneously.
This you have
aPointer name of object
* its a pointer
char its a char thats pointed to

the location of the whitespace is immaterial by the way.

0 new messages