|
-
March 3rd, 2007, 04:10 AM
#1
Professional placement of derefencing operator
Hey guys. Just wondering about something...
Code:
int* pA = NULL;
int * pB = NULL;
int *pC = NULL;
Where do MOST professional programmers place their derefence operators (*)?
Also, is intialising a pointer to NULL a bad practice/habit to get into?
Last edited by Mybowlcut; March 3rd, 2007 at 04:10 AM.
Reason: Changed Title
-
March 3rd, 2007, 04:34 AM
#2
Re: Professional placement of derefencing operator
1) It doesn't matter, don't concern yourself with it
2) Quite the contrary
Windows XP, Visual Studio 2008, SVN
-
March 3rd, 2007, 04:48 AM
#3
Re: Professional placement of derefencing operator
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.
-
March 3rd, 2007, 05:11 AM
#4
Re: Professional placement of derefencing operator
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.
-
March 3rd, 2007, 05:22 AM
#5
Re: Professional placement of derefencing operator
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
-
March 3rd, 2007, 05:29 AM
#6
Re: Professional placement of derefencing operator
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.
Also, real programmers don't use NULL:
-
March 3rd, 2007, 05:34 AM
#7
Re: Professional placement of derefencing operator
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)?
-
March 3rd, 2007, 05:41 AM
#8
Re: Professional placement of derefencing operator
 Originally Posted by 7stud
Also, real programmers don't use NULL:
This is not correct. I think that using NULL can often be more clear than using 0.
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;
Code:
#define NULL 0
#define NULL (10-10)
#define NULL 0L
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).
Look at this for more on null terminating strings; http://en.wikipedia.org/wiki/Null_character
-
March 3rd, 2007, 06:04 AM
#9
Re: Professional placement of derefencing operator
What placement do you prefer, laitinen?
-
March 3rd, 2007, 06:30 AM
#10
Re: Professional placement of derefencing operator
I prefer 0 to NULL, but that's purely personal.
As for placement of the "*", I put it with the variable:
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 3rd, 2007, 06:32 AM
#11
Re: Professional placement of derefencing operator
 Originally Posted by Mybowlcut
I want to know what YOU use and what CALCULATOR uses etc. :P
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.
-
March 3rd, 2007, 06:34 AM
#12
Re: Professional placement of derefencing operator
What about reference operators, Graham?
 Originally Posted by JohnyDog
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.
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. 
Hmm... so you like as well?
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.
Last edited by Mybowlcut; March 3rd, 2007 at 06:40 AM.
-
March 3rd, 2007, 08:18 AM
#13
Re: Professional placement of derefencing operator
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|