By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,483 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,483 IT Pros & Developers. It's quick & easy.

float* f vs float *f

P: n/a

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
Nov 8 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Tom Impelluso <im******@attila.sdsu.edukirjutas:
>
Hi!
I have used both of these
"float *f"
and
"float* f"

Could someone tell me if one is
preferred and why?
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.

YMMV
Paavo

Nov 8 '08 #2

P: n/a
On 2008-11-08 18:35, Paavo Helde wrote:
Tom Impelluso <im******@attila.sdsu.edukirjutas:
>>
Hi!
I have used both of these
"float *f"
and
"float* f"

Could someone tell me if one is
preferred and why?

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.

--
Erik Wikström
Nov 8 '08 #3

P: n/a
Erik Wikström wrote:
On 2008-11-08 18:35, Paavo Helde wrote:
>Tom Impelluso <im******@attila.sdsu.edukirjutas:
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.
And then whitespace, '=', whitespace, and initialisation value (possibly
NULL). Initialising each pointer when it is declared is a good practice.

Best wishes,

Zeppe
Nov 8 '08 #4

P: n/a
Tom Impelluso wrote:
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.

Nov 8 '08 #5

P: n/a
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.
Nov 9 '08 #6

P: n/a
Rolf Magnus wrote:
Tom Impelluso wrote:
>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.
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

Nov 9 '08 #7

P: n/a
On Nov 8, 11:11*pm, Rolf Magnus <ramag...@t-online.dewrote:
Tom Impelluso wrote:
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.
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:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
Nov 9 '08 #8

P: n/a
On Nov 9, 6:29*am, James Kanze <james.ka...@gmail.comwrote:
On Nov 8, 11:11*pm, Rolf Magnus <ramag...@t-online.dewrote:
Tom Impelluso wrote:
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.

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:james.ka...@gmail.com
Conseils en informatique oriente objet/
* * * * * * * * * *Beratung in objektorientierter Datenverarbeitung
9 place Smard, 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?
Nov 9 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.