float* f vs float *f

T

Tom Impelluso

Hi!


I have used both of these
"float *f"
and
"float* f"

Could someone tell me if one is
preferred and why? Yes, i know both
work but it makes me feel uneasy.

Ditto for:

FILE* fp
vs.
FILE *fp

I would hope to know if there is a standard
and the second is just allowed.

thanks
tom
 
E

Erik Wikström

It's a style issue. If you declare multiple pointers together, then this
looks better:

float *f, *g, *h;

than

float* f,* g,* h;

On the other hand, if you declare a single thing only, then IMHO

float* f;

looks better than

float *f;

Some people (not me) argue that this gives one reason to the idea to
always declare only a single thing at a time.

On the other hand, if you already subscribe to the idea of only one
declaration per line it becomes natural to write "float* f"; type,
whitespace, and then the name.
 
Z

Zeppe

Erik said:
On the other hand, if you already subscribe to the idea of only one
declaration per line it becomes natural to write "float* f"; type,
whitespace, and then the name.

And then whitespace, '=', whitespace, and initialisation value (possibly
NULL). Initialising each pointer when it is declared is a good practice.

Best wishes,

Zeppe
 
R

Rolf Magnus

Tom said:
Could someone tell me if one is
preferred and why? Yes, i know both
work but it makes me feel uneasy.

Ditto for:

FILE* fp
vs.
FILE *fp

I would hope to know if there is a standard
and the second is just allowed.

There is no standard. It seems to me that the first is more common in C++,
while the second is more common in C, but I've seen both in both languages.
I've also seen a third form, for the undecided:

FILE * fp;

I prefer the first version, since it's more natural to me. The * is part of
the type, and so it belongs to the type and not the name. The inventors of
C, howerver, seem to think that the other variant is more natural, since it
kind of matches with the dereferene operator, and you could say that *fp is
of type FILE.
 
R

red floyd

Rolf Magnus wrote:
The inventors of
C, howerver, seem to think that the other variant is more natural, since it
kind of matches with the dereferene operator, and you could say that *fp is
of type FILE.

That's how I finally grokked pointers in C, wayyyyy back in the day.
 
B

Bo Persson

Rolf said:
There is no standard. It seems to me that the first is more common
in C++, while the second is more common in C, but I've seen both in
both languages. I've also seen a third form, for the undecided:

FILE * fp;

I prefer the first version, since it's more natural to me. The * is
part of the type, and so it belongs to the type and not the name.
The inventors of C, howerver, seem to think that the other variant
is more natural, since it kind of matches with the dereferene
operator, and you could say that *fp is of type FILE.

This fails for C++ references, where

int i = 42;

int& r = i;
and
int &r = i;

are equivalent, but we can't say that &r is of type int.

So to be consistent, you might want to use the style
type-space-name-initializer whenever possible:

int& r = i;

int* p = &i;


Bo Persson
 
J

James Kanze

There is no standard. It seems to me that the first is more
common in C++, while the second is more common in C, but I've
seen both in both languages. I've also seen a third form, for
the undecided:
FILE * fp;
I prefer the first version, since it's more natural to me. The
* is part of the type, and so it belongs to the type and not
the name. The inventors of C, howerver, seem to think that the
other variant is more natural, since it kind of matches with
the dereferene operator, and you could say that *fp is of type
FILE.

That was the original philosophy behind C's declaration syntax;
you specified the basic type, and then an expression which
denoted the basic type. It broke, of course, the day they
introduced typedef's and struct. It broke again when const was
introduced. In sum, it was an experiment that failed, but that
we still have to live with. What it does mean is that we get a
lot of ambiguities between expressions and declarations, and
that there is one more reason to reject more than one
declaration per statement.
 
B

Bharath

That was the original philosophy behind C's declaration syntax;
you specified the basic type, and then an expression which
denoted the basic type.  It broke, of course, the day they
introduced typedef's and struct.  It broke again when const was
introduced.  In sum, it was an experiment that failed, but that
we still have to live with.  What it does mean is that we get a
lot of ambiguities between expressions and declarations, and
that there is one more reason to reject more than one
declaration per statement.

--
James Kanze (GABI Software)             email:[email protected]
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Check here: http://www.research.att.com/~bs/bs_faq2.html
see question: Is ``int* p;'' right or is ``int *p;'' right?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top