Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A religious question: int* i; /*or*/ int *i;?

Reply
Thread Tools

A religious question: int* i; /*or*/ int *i;?

 
 
=?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRoZSBv76yDY2XigKYpIg==?=
Guest
Posts: n/a
 
      08-27-2004
Here my religious answer...............


Typically in my code I write:

int * abc;
char de;
my_type fgh;
int ijkl;
int * mn;
my_long_type opq;
my_type * rstu;
my_long_type * vw;
my_long_type xyz;

My rules (used in the last 20 years):

type variable_name1; /* Only 1 variable per declaration */
type * variable_name2; /* Note the space between type and * */
very_long_type variable_name3; /* All variable-name aligned */


- Dario
 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      08-27-2004

"Ioannis Vranos" <> wrote in message
news:cgmn3s$1aar$...
> Howard wrote:
>
> > This has been discussed so many times it's ridiculous. But, seeing as

I'm
> > the ultimate "notable authority" (from my perspective, anyway ), I
> > declare that the CORRECT way is to put the * with the type, not with the
> > variable name. Plus, NEVER declare more than one variable on one line!
> > (That prevents any chance of confusion, and makes finding the variable
> > you're looking for much easier.)

>
>
> However if you declare many variables in the same line, the * with the
> variable name is the more reasonable and obvious. Also when you declare
> a pointer alone, the * going with the variable name is the more
> reasonable too.
>


That's where I disagree. I think "int* p" declares p to be a pointer-to-int
much better than "int *p", which looks more like a dereferencing of p.

>
> How do you dereference by the way * p=7; or *p=7; ?


I use *p = 7; , but that's the dereference operator being applied, not a
declaration.

-Howard


 
Reply With Quote
 
 
 
 
XZ
Guest
Posts: n/a
 
      08-27-2004
>
> If you only declare -- and initialise -- variables at the point of first
> use, most of these multiple declarations vanish, so the question doesn't
> arise .


However, that wouldn't work in C.
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      08-27-2004

"Ioannis Vranos" <> wrote in message
news:cgnavo$2ngr$...
> Denis Remezov wrote:
>
> > Whichever we happen to like.
> >
> > Most generally, you cannot declare multiple names of /different/
> > types in one declaration.
> >
> > int signed s, unsigned u; //illegal
> >
> > This is why I see no value (or elegance) in being able to declare
> > variables of type T and T* (and T**, ...) in one declaration.

>
>
> You have got the main issue. Since int and int * are different types the
> standard should require an error. That's why we are here with this
> syntax stuff today.
>
>
> However based on the fact that they are allowed to be declared together,
> and the asterisk denotes a pointer variable, while an object next to it
> in the same declaration without an asterisk becomes a non-pointer
> object, the most rational thing in this kind of declarations is the
> asterisk to go close to the pointer variable itself rather than the type.
>
>


To me, the rational thing to do is to forbid my staff from declaring
multiple variables on one line in the first place, regardless of their
types. And since I have a staff of one (me), I can enforce that requirement
quite easily! (I rarely disobey myself these days.)

-Howard



 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      08-27-2004
XZ wrote:

> > If you only declare -- and initialise -- variables at the point of first
> > use, most of these multiple declarations vanish, so the question doesn't
> > arise .

>
> However, that wouldn't work in C.


As quoted, C has permitted declaration and simultaneous initialization of
variables...

int x = 0;

....since the 1970s.

Were you trying to say something else?

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


 
Reply With Quote
 
Rich Grise
Guest
Posts: n/a
 
      08-27-2004
"Dario (drinking co?ee in the o?ce?)" wrote:

> Here my religious answer...............
>
>
> Typically in my code I write:
>
> int * abc;
> char de;
> my_type fgh;
> int ijkl;
> int * mn;
> my_long_type opq;
> my_type * rstu;
> my_long_type * vw;
> my_long_type xyz;
>
> My rules (used in the last 20 years):
>
> type variable_name1; /* Only 1 variable per declaration */
> type * variable_name2; /* Note the space between type and * */
> very_long_type variable_name3; /* All variable-name aligned */
>


Well, that makes sense, because, after all, abc is a data item whose type
is pointer. *abc refers to its contents. So, strictly, int *abc doesn't mean
anything. Or shouldn't. Well, it was that way 20 years ago, please correct
me if I'm wrong - I haven't read the actual Spec yet. Is this Spec available
on the net somewhere, or do I need to piece it together from tutorials and
NG stuff? Buying a book isn't in the budget this week, for the same reason
that all my time is free for learning stuff.

But this is a C++ group - are pointers really that rampant? I'd have thought
a reference would be more in the spirit of OOps? But I'm a C++ noob, and
learned to cut-n-paste C by guess & by gosh, with K&R at my side.

Thanks,
Rich

 
Reply With Quote
 
Rich Grise
Guest
Posts: n/a
 
      08-27-2004
Ioannis Vranos wrote:

> Denis Remezov wrote:
>
>> Whichever we happen to like.
>>
>> Most generally, you cannot declare multiple names of /different/
>> types in one declaration.
>>
>> int signed s, unsigned u; //illegal
>>
>> This is why I see no value (or elegance) in being able to declare
>> variables of type T and T* (and T**, ...) in one declaration.

>
>
> You have got the main issue. Since int and int * are different types the
> standard should require an error. That's why we are here with this
> syntax stuff today.
>
>
> However based on the fact that they are allowed to be declared together,
> and the asterisk denotes a pointer variable, while an object next to it
> in the same declaration without an asterisk becomes a non-pointer
> object, the most rational thing in this kind of declarations is the
> asterisk to go close to the pointer variable itself rather than the type.
>

No it's not. The most rational thing is to declare one item, or at most
one type, per declaration, IM!HO.

Cheers!
Rich

 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      08-28-2004
Phlip wrote:
> XZ wrote:
>
>
>>>If you only declare -- and initialise -- variables at the point of first
>>>use, most of these multiple declarations vanish, so the question doesn't
>>>arise .

>>
>>However, that wouldn't work in C.

>
>
> As quoted, C has permitted declaration and simultaneous initialization of
> variables...
>
> int x = 0;
>
> ...since the 1970s.
>
> Were you trying to say something else?



He was probably talking about C90 where variable definitions could occur
only in the beginning of a scope.


However in C99 the can be defined anywhere in a scope, as in C++98.
However I still tend to define all objects in the beginning of scopes
with the exception of for loop counters.






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      08-28-2004
Rich Grise wrote:

> No it's not. The most rational thing is to declare one item, or at most
> one type, per declaration, IM!HO.



Why? Do you mean you declare stuff in the style:


int i;
int j;
const int WHATEVER=7;


etc?



Myself use this style:


int i, j, *pName, value;

const int WHATEVER=7;


or


class whatever
{
string firstName, secondName;
// ...
};






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Mabden
Guest
Posts: n/a
 
      09-01-2004
"Ioannis Vranos" <> wrote in message
news:cgpl29$avm$...
> Rich Grise wrote:
>
> > No it's not. The most rational thing is to declare one item, or at most
> > one type, per declaration, IM!HO.

>
> Why? Do you mean you declare stuff in the style:
>
> int i;
> int j;
> const int WHATEVER=7;
> etc?
>
> Myself use this style:
> int i, j, *pName, value;
>
> const int WHATEVER=7;


Well, I find that sloppy. I use a mixture as follows:

int i, j, k; // loop stuff and temps
int max; // max value of that thing
int *ptr; // reason I'm pointing at you

So in general, throw-away ints are listed together, but stuff that means
something gets its own line with a comment about what it is for.

Remember, people, we are primarily writers. You are generating script for
the next programmer (who may be you) to understand and act upon. Be generous
in explanation. The compiler won't mind.

--
Mabden


 
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
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57