PDA

View Full Version : coding style



mickey
16th March 2008, 17:45
Hello,
I read a bit thinking in C++ and other book and I see that for declare pointer the style more used is int* y; then I read the Ansi C Ritchie's book and I say it use int *y; what the best tyle?
Actually, it seems better the second; for example: "int *y, *z" should be ok but not "int* y, z" (this last it's not the same). I read the '*' is referer to name variabile and not the type.
What do you think?

Gopala Krishna
16th March 2008, 18:14
Actually it doesn't matter when you are declaring only one pointer variable. (which is why bruce probably didn't bother to use the latter version)

When you are declaring multiple pointer variables in a single declarations then
int *x, *y; is better since it conveys the meaning more precisely and also hammers the fact that int* is not carried to all variables after the first one.

przemoc
16th March 2008, 18:19
Making many variable declarations in one line is less readable and error-prone, especially if you use pointers and references with reference symbol next to the type, which is common in C++ sources, as you already noticed.

You can read moderately popular C++ Programming Style Guidelines by Geotechnical Software Services (http://geosoft.no/development/cppstyle.html).
51th recommendation says:

The pointer-ness or reference-ness of a variable is a property of the type rather than the name.

I think that above approach is right and I follow it when writting in C++, but not in C.