473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

Sal
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.
Jun 27 '08 #1
17 1155
On May 29, 5:20 pm, Sal <h...@softcom.netwrote:
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.
Personally I prefer
int * n to denote a pointer to an integer.
int & n to denote a reference to an integer.
&n to denote the address of n.
*n to denote dereferencing of a pointer named n.
Jun 27 '08 #2
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
Jun 27 '08 #3
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;

With that thinking you'd also think that you are creating two
int-pointers above, when in fact you are not (you are creating an
int-pointer named p1 and an int variable named p2).

Correcting that while still keeping the intuition is not easy. What I
personally usually do is to just avoid the problem and use two lines:

int* p1;
int* p2;

It's more verbose, though.
Jun 27 '08 #4

"Juha Nieminen" <no****@thanks.invalidwrote in message
news:1X***********@read4.inet.fi...
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;

With that thinking you'd also think that you are creating two
int-pointers above, when in fact you are not (you are creating an
int-pointer named p1 and an int variable named p2).

Correcting that while still keeping the intuition is not easy. What I
personally usually do is to just avoid the problem and use two lines:

int* p1;
int* p2;

It's more verbose, though.
Nothing wrong with verbosity!

;^D

Jun 27 '08 #5
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.
Jun 27 '08 #6
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.

--
Erik Wikström
Jun 27 '08 #7
Erik Wikström <Er***********@telia.comwrites:
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.
Yes, that's why you should neve declare more than one variable at once in C/C++.

int* p1;
int* p2;

Or better, choose another programming language. Are we all
implementing kernel level programs? I doubt it.

--
__Pascal Bourguignon__
Jun 27 '08 #8
On 30 Maj, 17:18, Matthias Buelow <m...@incubus.dewrote:
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 ;-)
The problem with your pointer-definition is the loss of flexibility.
You would now need another typedef for at pointer to const int and so
would end up with tow definitions for every pointer. Also, the int_ptr
is in my opinion less readable: at least, this convention will take a
little while getting used to.

/Peter
Jun 27 '08 #9
Pascal J. Bourguignon wrote:
Erik Wikström <Er***********@telia.comwrites:
>On 2008-05-30 09:22, Juha Nieminen wrote:
>>int* p1, p2;
What I find embarrassing is the fact that p2 is not a pointer, what kind
of logic is that.
the logic of the language designers.

If you write

int *p1, p2;

the meaning is clear. This is why fluency in the language is critical if
you are going to use it for anything significant. If you have difficulty
distinguishing between variable declaration and dereference operators,
you're going to have a great deal of trouble getting things to work.
This is not meant to flame, that's just how it is.
Yes, that's why you should neve declare more than one variable at once in C/C++.
a.) See above.
b.) One can make the argument that you should never repeat yourself. If
a set of variables *must* be the same type for code to make sense [i.e.
assignments and such between them], then one should declare them with
the same type identifier. For example,

float *matrixA,
*matrixB,
*matrixC;

is preferable to

float *matrixA;
float *matrixB;
float *matrixC;

and is helpful when you want to change the type to, say, double.
Clearly, using C++ reduces the need to maintain pointers of things [you
could have a matrix class of sorts: matrix<floatA, B, C; ].

The bottom line is when intuition yields results that are incompatible
with reality, you must move beyond what your intuition offers.
Or better, choose another programming language. Are we all
implementing kernel level programs? I doubt it.
C++ is quite powerful, especially when combined with a decent set of
libraries [like Boost]. One need never write C code unless a C++
compiler for the target architecture doesn't exist.

--
Andrew Kerr
Jun 27 '08 #10
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.
It's an unpopular opinion, but for me "int* n" looks like denial. Maybe it
would be nice if the * would bind to the int and you could write
Â* int* p1, p2; Â*// two pointers to int
but, like it or not, C++ does not work that way. I prefer the notation that
reflects reality (int *p1, *p2 (or int &p1, &p2)).

You wouldn't write "a+b * c" either, would you?
Jun 27 '08 #11
On May 30, 8:44 pm, peter koch <peter.koch.lar...@gmail.comwrote:
On 30 Maj, 17:18, Matthias Buelow <m...@incubus.dewrote:
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:ja*********@gmail.com
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
Jun 27 '08 #12
On May 30, 6:02 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
Erik Wikström <Erik-wikst...@telia.comwrites:
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:ja*********@gmail.com
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
Jun 27 '08 #13
On 31 Maj, 11:02, James Kanze <james.ka...@gmail.comwrote:
On May 30, 8:44 pm, peter koch <peter.koch.lar...@gmail.comwrote:
On 30 Maj, 17:18, Matthias Buelow <m...@incubus.dewrote:
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
Jun 27 '08 #14
On May 29, 11:20*pm, Sal <h...@softcom.netwrote:
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.

Jun 27 '08 #15
On May 31, 12:13 pm, peter koch <peter.koch.lar...@gmail.comwrote:
On 31 Maj, 11:02, James Kanze <james.ka...@gmail.comwrote:
On May 30, 8:44 pm, peter koch <peter.koch.lar...@gmail.comwrote:
On 30 Maj, 17:18, Matthias Buelow <m...@incubus.dewrote:
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:ja*********@gmail.com
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
Jun 27 '08 #16
"Pascal J. Bourguignon" <pj*@informatimago.comwrote in message
news:7c************@pbourguignon.anevia.com...
Erik Wikström <Er***********@telia.comwrites:
>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.

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

int* p1;
int* p2;

Or better, choose another programming language. Are we all
implementing kernel level programs? I doubt it.
Or high-end flight control systems for state-of-the-art military aircraft:

http://www.research.att.com/~bs/JSF-AV-rules.pdf

IMHO, C++ is a very diverse and can be used to solve basically any
programming problem...

Jun 27 '08 #17
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
Jun 27 '08 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Goran Pusic | last post by:
Hi all! I understand the rationale behind the warning 4239 (I think :-)) (warning C4239: nonstandard extension used : 'argument' : conversion from X to Y A reference that is not to 'const'...
8
by: ashu | last post by:
lets take a look at the following code:- #include<stdio.h> #include<conio.h> struct tag{ int age; char *name; }a;
1
by: ken.carlino | last post by:
Hi, I have the following compile error instantiated from here, I appreciate if someone can help me find out why? g++ -O0 -g3 -Wall -c -fmessage-length=0 -ostddev.o ../stddev.cpp...
3
by: K B | last post by:
Hi again, I've narrowed down my problem and am hoping to get some help to get the final answer. Gridview with dynamic child controls loads gv1.Databind calls RowDataBound event fine On the...
6
by: de_mystiX | last post by:
I've been searching these forums and trying to figure this out on my own but am having problems. int x; cin >> x; if a noninteger value is entered the program goes into an endless loop - is...
28
by: Frederick Gotham | last post by:
When I was a beginner in C++, I struggled with the idea of references. Having learned how to use pointers first, I was hesitant to accept that references just "do their job" and that's it. Just...
6
by: Charles Sullivan | last post by:
I define and initialize an array of structures like the following, (where the <verbiage within angle bracketsis just meant to be explanatory): int func1(<argument prototypes>); int...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
2
by: raylopez99 | last post by:
Here is a short program demonstrating using IEnumberable, Linq, predicates, extension methods and some tricks and how to convert an IEnumberable sequence into an array. For future reference, not...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.