Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Here we go again: int *n or int* n

Reply
Thread Tools

Here we go again: int *n or int* n

 
 
James Kanze
Guest
Posts: n/a
 
      05-31-2008
On May 30, 8:44 pm, peter koch <(E-Mail Removed)> wrote:
> On 30 Maj, 17:18, Matthias Buelow <(E-Mail Removed)> wrote:


> > Juha Nieminen wrote:
> > > int* p1;
> > > int* p2;
> > > It's more verbose, though.


> > One could typedef int *int_ptr; and then use that. The
> > question is, why isn't that used (more often)? Seems the
> > *-quirk is elegant enough for daily use and doesn't cause
> > many problems.


> I believe that one variable per declaration is a good idea.
> The verbosity you add is not worth bothering about. I never
> met a programmer that could program as ifast as he could type,
> so there is no bottleneck here


I have, but that's because they were slow typers, and poor
programmers (who didn't think things out first). Realistically,
however, C++ does contain a lot of boiler plate, so while you
will spend more time thinking about what you should write than
actually writing it, once you've thought about it and know what
is needed, typing can be a bottleneck to getting it down on
disk. A programmer who can't touch type is missing an important
productivity tool.

> The problem with your pointer-definition is the loss of
> flexibility.


The problem with the typedef for a pointer is the loss of
readability. The purpose of a typedef is abstraction; normally,
you don't want to abstract out the fact that you're dealling
with a pointer, since anyone using the type must be aware of it.
(A typedef to a pointer is valid if you expect the pointer to be
used as an opaque handle; in other words, if the client code
should not count on it being a pointer.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      05-31-2008
On May 30, 6:02 pm, (E-Mail Removed) (Pascal J. Bourguignon)
wrote:
> Erik Wikström <(E-Mail Removed)> writes:
> > On 2008-05-30 09:22, Juha Nieminen wrote:
> >> Chris Gordon-Smith wrote:
> >>> I don't know BS's reasoning, but I read "int* n" as "n is
> >>> an integer pointer".


> >>> "int *n" seems to say "If you dereference n, you get to
> >>> an integer".


> >>> The former seems more intuitive to me.


> >> It may be more intuitive (and is what I use as well, for
> >> that reason), but thinking about it like that causes an
> >> embarrassing problem:


> >> int* p1, p2;


> > What I find embarrassing is the fact that p2 is not a
> > pointer, what kind of logic is that.


The same logic which makes you put part of the type information
after the name, e.g. "int a[10]" (rather than "array [10] of
int").

> Yes, that's why you should neve declare more than one variable
> at once in C/C++.


> int* p1;
> int* p2;


That's a good rule in general, in any language.

> Or better, choose another programming language.


But which one. C++ has a lot of problems (especially with
declaration syntax), and is really a horrible language when it
comes down to it. But all of the others I've seen are even
worse.

--
James Kanze (GABI Software) email:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      05-31-2008
On 31 Maj, 11:02, James Kanze <(E-Mail Removed)> wrote:
> On May 30, 8:44 pm, peter koch <(E-Mail Removed)> wrote:
>
> > On 30 Maj, 17:18, Matthias Buelow <(E-Mail Removed)> wrote:
> > > Juha Nieminen wrote:
> > > > int* p1;
> > > > int* p2;
> > > > * It's more verbose, though.
> > > One could typedef int *int_ptr; and then use that. The
> > > question is, why isn't that used (more often)? Seems the
> > > *-quirk is elegant enough for daily use and doesn't cause
> > > many problems.

> > I believe that one variable per declaration is a good idea.
> > The verbosity you add is not worth bothering about. I never
> > met a programmer that could program as ifast as he could type,
> > so there is no bottleneck here

>
> I have, but that's because they were slow typers, and poor
> programmers (who didn't think things out first). *Realistically,
> however, C++ does contain a lot of boiler plate, so while you
> will spend more time thinking about what you should write than
> actually writing it, once you've thought about it and know what
> is needed, typing can be a bottleneck to getting it down on
> disk. *A programmer who can't touch type is missing an important
> productivity tool.


This is funny, because I remember having used some editors that did
contain macros for generating the boilerplate code. So pressing CTRL-I
created an if statement, CTRL-F a function - placing the cursor at the
"correct" place - e.g. after the "(" for the if-statement. I never
found it worthwhile to learn those shortcuts and I'm a rather poor
typist.
For the problem with two similar declarations, I write the first
declarations, copy it and change the name of the variable (and
probably the initialisation) in the second declaration.

>
> > The problem with your pointer-definition is the loss of
> > flexibility.

>
> The problem with the typedef for a pointer is the loss of
> readability. *The purpose of a typedef is abstraction; normally,
> you don't want to abstract out the fact that you're dealling
> with a pointer, since anyone using the type must be aware of it.


I mentioned that in the part you snipped away. The reason I did not
emphasize it so much is because it really does not take that long to
get used to the convention that appending _pointer (or _const_pointer)
to a name declares a pointer to the type of the first part of the
name. That it requires a little bending to call it an abstraction is
not a problem for me. typedefs are used for different stuff than
abstractions anyway.

/Peter
 
Reply With Quote
 
Tomás Ó hÉilidhe
Guest
Posts: n/a
 
      05-31-2008
On May 29, 11:20*pm, Sal <(E-Mail Removed)> wrote:
> I'm aware of all the religious arguments of the former over the
> latter, but does anyone know why Bjarne Stroustrup seems to prefer
> int* n ? Dumb question, but I just had to ask.



I use:

int *p;

because it accurately reflects the grammar of both C and C++.

A normal declaration looks something like as follows:

Type name;

or:

Type name1, name2, name3;

The complication to this simple scheme, however, is that you can have
pointers and references. If you want to make a pointer out of
something, you add the asterisk to its _name_, not to its type. Same
goes for arrays, you add the [N] to the name, not the type. That's why
we have declarations such as:

int (*pFunc)(int,int);

I suggest strongly against taking the asterisk and putting it with the
typename:

int* p;

because it goes against the grammar.

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-31-2008
On May 31, 12:13 pm, peter koch <(E-Mail Removed)> wrote:
> On 31 Maj, 11:02, James Kanze <(E-Mail Removed)> wrote:
> > On May 30, 8:44 pm, peter koch <(E-Mail Removed)> wrote:


> > > On 30 Maj, 17:18, Matthias Buelow <(E-Mail Removed)> wrote:
> > > > Juha Nieminen wrote:
> > > > > int* p1;
> > > > > int* p2;
> > > > > It's more verbose, though.
> > > > One could typedef int *int_ptr; and then use that. The
> > > > question is, why isn't that used (more often)? Seems the
> > > > *-quirk is elegant enough for daily use and doesn't cause
> > > > many problems.
> > > I believe that one variable per declaration is a good idea.
> > > The verbosity you add is not worth bothering about. I never
> > > met a programmer that could program as ifast as he could type,
> > > so there is no bottleneck here


> > I have, but that's because they were slow typers, and poor
> > programmers (who didn't think things out first).
> > Realistically, however, C++ does contain a lot of boiler
> > plate, so while you will spend more time thinking about what
> > you should write than actually writing it, once you've
> > thought about it and know what is needed, typing can be a
> > bottleneck to getting it down on disk. A programmer who
> > can't touch type is missing an important productivity tool.


> This is funny, because I remember having used some editors
> that did contain macros for generating the boilerplate code.


Some of the boiler plate is usually generated automatically by
the editor. I've never worked on a system where the editor
didn't automatically generate the copyright, and the include
guards for a .h, for example, and it's pretty frequent for them
to automatically generate the basic structure of a class
declaration. On the other hand, I've never seen one that would
automatically generate all of the forwarding constructors. Nor,
for that matter, which would generate empty function bodies for
the function implementations (in another file), given the class
definition.

> So pressing CTRL-I created an if statement, CTRL-F a function
> - placing the cursor at the "correct" place - e.g. after the
> "(" for the if-statement. I never found it worthwhile to learn
> those shortcuts and I'm a rather poor typist.


I've never found those worthwhile either. A macro that, when
the cursor is placed on a function in a header file, would
create a new file with an empty definition for the function
would be useful, however.

> For the problem with two similar declarations, I write the
> first declarations, copy it and change the name of the
> variable (and probably the initialisation) in the second
> declaration.


Copy/paste can help a lot, I agree.

--
James Kanze (GABI Software) email:(E-Mail Removed)
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
 
Reply With Quote
 
Joshua Brinsfield
Guest
Posts: n/a
 
      06-02-2008
Chris Gordon-Smith wrote:
> Sal wrote:
>
>> I'm aware of all the religious arguments of the former over the
>> latter, but does anyone know why Bjarne Stroustrup seems to prefer
>> int* n ? Dumb question, but I just had to ask.

>
> I don't know BS's reasoning, but I read "int* n" as "n is an integer
> pointer".
>
> "int *n" seems to say "If you dereference n, you get to an integer".
>
> The former seems more intuitive to me.
>
> That being said, Item 0 of Sutter and Alexandrescu's "C++ Coding Standards"
> says "Don't Sweat the Small Stuff". In other words, don't worry too much
> about thingsa that really are just a matter of taste.
>
> Chris Gordon-Smith
> www.simsoup.info


The intuition-wise justification for the other ("int *n;") is arrived at
by thinking of * only as a dereference operator: then you have "int
(*n);", which should declare the value of n dereferenced as an
int--which would implicitly declare n as a pointer to int. And since
there's no typename like "pointer<int>" we can use, then we don't have
any way to do pointer declarations explicitly; and so it would make
sense to do it implicitly through the dereference operator.

Then it makes sense and your intuitive revulsion can lie dormant,
enjoying a full belly of intellectual kluge .

-Brinsfield
 
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