Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > float* f vs float *f

Reply
Thread Tools

float* f vs float *f

 
 
Tom Impelluso
Guest
Posts: n/a
 
      11-08-2008

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


 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      11-08-2008
On 2008-11-08 18:35, Paavo Helde wrote:
> Tom Impelluso <(E-Mail Removed)> kirjutas:
>
>>
>> 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
 
Reply With Quote
 
 
 
 
Zeppe
Guest
Posts: n/a
 
      11-08-2008
Erik Wikström wrote:
> On 2008-11-08 18:35, Paavo Helde wrote:
>> Tom Impelluso <(E-Mail Removed)> kirjutas:
>> 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
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      11-08-2008
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.

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      11-09-2008
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.


 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      11-09-2008
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



 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-09-2008
On Nov 8, 11:11*pm, Rolf Magnus <(E-Mail Removed)> 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.


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:(E-Mail Removed)
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
 
Reply With Quote
 
Bharath
Guest
Posts: n/a
 
      11-09-2008
On Nov 9, 6:29*am, James Kanze <(E-Mail Removed)> wrote:
> On Nov 8, 11:11*pm, Rolf Magnus <(E-Mail Removed)> 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.

>
> 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:(E-Mail Removed)
> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
operator== (float, float) Jukka Lehtonen C++ 5 08-05-2004 08:28 AM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
static_cast<float>(a) versus float(a) Jim West C++ 4 01-16-2004 12:36 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments