Hey guys. Just wondering about something...
Where do MOST professional programmers place their derefence operators (*)?Code:
int* pA = NULL;
int * pB = NULL;
int *pC = NULL;
Also, is intialising a pointer to NULL a bad practice/habit to get into?
Printable View
Hey guys. Just wondering about something...
Where do MOST professional programmers place their derefence operators (*)?Code:
int* pA = NULL;
int * pB = NULL;
int *pC = NULL;
Also, is intialising a pointer to NULL a bad practice/habit to get into?
1) It doesn't matter, don't concern yourself with it
2) Quite the contrary
Thanks for answering. :)
But I'm interested to know... I know that nearly everyone who is usually answering people's questions here is a professional developer or hobbyist etc since they have so much knowledge of C++... and I wanna know what most people here and in work situations use. :)
I think I should concern myself with it because it will get me into good coding habits.
If you are working on project with multiple people, follow their coding style. If you are writing program by yourself, use the style that suits you best. The purpose of indentation is to make the program easily readable - some people use 'int *a' to let the base type (int) stands out, some use 'int* a' as it better follows the logic (the type is whole "pointer to int"), yet others use 'int * a' to make the fact that it is pointer stand out, as they may have otherwise overlook it while reading the code. If you don;t have opinion about this, choose one style at random, _stick to it_ and see if it works for you.
I don't want to choose a random style though. :P I want to know what YOU use and what CALCULATOR uses etc. :P I'm asking the advanced C++'ers to tell me what they use... I would start a poll but I don't think I can and I only just thought of it then. Haha.
Oh, and thanks for that second answer Calculator. :P
I use the syntax:
because it makes the most sense to me: the type is pointer to int and the variable name is ptr, and that syntax neatly separates the two.Code:int* ptr;
Also, real programmers don't use NULL:
Code:int* ptr = 0;
Thanks for your input, 7 :).
Why don't they use NULL? I think I read somewhere that NULL was better..? And aren't they the same thing (0)?
This is not correct. I think that using NULL can often be more clear than using 0.Quote:
Originally Posted by 7stud
NULL is a macro which in c++ usually is defined as 0 or 0L. Or to be more specific it is an integer constant expression that evaluates to 0.
This means that it is implementation specific, and these would be valid definitions for the NULL macro;
Note that NULL cannot be defined as ((void*)1). In C++ it cant be defined as a pointer at all (which it can in C, as an integer const expression that evals to 0 being casted to a pointer to void).Code:#define NULL 0
#define NULL (10-10)
#define NULL 0L
Look at this for more on null terminating strings; http://en.wikipedia.org/wiki/Null_character
What placement do you prefer, laitinen?
I prefer 0 to NULL, but that's purely personal.
As for placement of the "*", I put it with the variable:
Code:int *a, b;
The problem with the second one is that it's misleading: b is not a pointer. Of course, if you never declare multiple variables in one declaration statement, the point becomes moot.Code:int* a, b;
As Calculator already told you, it doesn't really matter :) For my projects i'm using 'int *a' as i've found that most prevalent in the tons of existing code i was dealing with in my career.Quote:
Originally Posted by Mybowlcut
What about reference operators, Graham?
Ah! Geez I know, I know already... It doesn't matter; it doesn't affect the program, but I already posted about my reasons for asking this question. :(Quote:
Originally Posted by JohnyDog
Hmm... so you likeas well?Code:int *a
I thought more people would use the other version TBH. Another reason I asked this is because I want to be consistent by using ONE approach - that applies to reference and dereference operators - to coding, so that my code is easy to read not only for me. :thumb:
Also see Stroustrup's FAQ
http://www.research.att.com/~bs/bs_faq2.html#null
http://www.research.att.com/~bs/bs_faq2.html#whitespace