Why int *foo instead of int* foo?
#1
Posted 04 June 2007 - 02:54 PM
It seems logical to me that a declaration of this sort should go:
<Type><Space><Identifier>;
The * is part of the type, not part of the name.
When you say int *foo you are creating a variable "foo" that is of type pointer-to-int or as I would see it "int*".
Is it just become one of those habit things that have continued for consistency's sake or is there an actual reason that the * goes with the identifier instead of the type?
#2
Posted 04 June 2007 - 03:03 PM
#3
Posted 04 June 2007 - 03:14 PM
int* p, q;
#4
Posted 04 June 2007 - 03:23 PM
tbp said:
int* p, q;
Ahh, I see.
IMO int* p,q should create 2 pointers, not a pointer and an int. That seems more logical to me.
#5
Posted 04 June 2007 - 04:26 PM
int * foo;
-
Currently working on: the 3D engine for Tomb Raider.
#6
Posted 04 June 2007 - 08:12 PM
poita said:
So you could regard the explicit indirection as more closely related to the way the variable name is used, than the type itself. int is the type, and *foo is how we access it.
Of course, these arguments are just 'excuses' to explain the somewhat odd semantics of what tbp illustrated... But it works for me! :lol:
#7
Posted 04 June 2007 - 08:14 PM
.oisyn said:
int * foo;^_^
#8
Posted 04 June 2007 - 08:25 PM
-
Currently working on: the 3D engine for Tomb Raider.
#9
Posted 04 June 2007 - 08:33 PM
typedef char* foo; const foo x; typedef const char bar; bar* y;to add another confusion: is x the same type as y? ;)
just another example of confusing C++ syntax, it makes a lot more sense if you change where you place your const.
typedef char* foo; foo const x; typedef char const bar; bar* y;
#10
Posted 04 June 2007 - 09:46 PM
#11
Posted 05 June 2007 - 02:24 PM
poita said:
IMO int* p,q should create 2 pointers, not a pointer and an int. That seems more logical to me.
This demonstrates that the compiler parses the star (*) as part of the identifier, and not as part of the type.
The fact that no syntax error is raised when compiling int* Foo; is a bonus, although welcome.
Yet, I'd like to see modern compilers featuring the option to raise a syntax error (at least a warning) when [type]* [identifier], or the opposite, is found.
It would help to enforce the same coding convention throughout a team project.
Ciao ciao : )
(readin' this? you ought to get out more)
#12
Posted 05 June 2007 - 02:39 PM
dave_ said:
typedef char* foo; const foo x; typedef const char bar; bar* y;to add another confusion: is x the same type as y?
const bar* z;and what is z?
-
Currently working on: the 3D engine for Tomb Raider.
#13
Posted 08 June 2007 - 02:29 AM
#14
Posted 08 June 2007 - 07:51 AM
#15
Posted 08 June 2007 - 09:43 AM
You can't alter an array - only it's elements. So there's no such thing as a const array of non-const elements or vice versa, that doens't make any sense. The same with references. You can't alter references, so const references don't exist.
-
Currently working on: the 3D engine for Tomb Raider.
#16
Posted 08 June 2007 - 08:59 PM
class A
{
public:
virtual int get() = 0;
};
class B
{
public:
int get()
{
return 5;
}
};
class C
{
public:
int get()
{
return 8;
}
};
int main()
{
int len = 10;
(A&)* arr = new (A&)[len]; //?
}
#17
Posted 08 June 2007 - 09:58 PM
-
Currently working on: the 3D engine for Tomb Raider.
#18
Posted 10 June 2007 - 09:25 AM
void foo(a, b, c)
int a, b;
float c;
{
...
}
the logic must have been that the function gets n parameters of type int. Wheather the variables are pointers or arrays then was seen as additional information to the type, that goes with the variable name. But that's probably a bad explanation for something purely concidental as well :)
#19
Posted 25 June 2007 - 06:35 PM
int* p; // pointer (to int) named p
I don't think like this:
int *p; // int (pointer to named p)
It's simple as that. I never declare more than one variable per line, so I get around the usual trap of:
int* a, b;
I never write..
int i, j;
.. either. I have a habit of initializing the variables, if possible, at declaration-- this comes from serious trauma at childhood: I learned assembly (z80) pretty much as the first programming language.
Second, when I (have to, like, at work) write C, I _try_ to initialize at declaration because I don't want to separate variables from their initialization by dozen or more lines of code. If there is that much code between the variable and assigning to one, then the function probably is too large anyway (or maybe not).
There is only one enemy (when writing C) to this scheme; asserts. Often you want to assert function arguments, well, duh?
void example(foo* bar)
{
assert( bar .. blablablablaaa );
int size = xxxComputeFapFactor(bar);
^ Anyone care to notice the error in above example? Oh yes, the assert before declaration. Question arises? Why not leave the assert to the xxxComputeFapFactor() then? A: becuase this is a *****ing example!
:) :)
#20
Posted 25 June 2007 - 08:00 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












