CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Post 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
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  2. #2
    Join Date
    Aug 2005
    Posts
    478

    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

  3. #3
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  4. #4
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    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.

  5. #5
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  6. #6
    Join Date
    Oct 2004
    Posts
    296

    Re: Professional placement of derefencing operator

    I use the syntax:
    Code:
    int* ptr;
    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:
    Code:
    int* ptr = 0;

  7. #7
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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)?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  8. #8
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Professional placement of derefencing operator

    Quote Originally Posted by 7stud
    Also, real programmers don't use NULL:
    Code:
    int* ptr = 0;
    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

  9. #9
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Professional placement of derefencing operator

    What placement do you prefer, laitinen?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  10. #10
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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:
    Code:
    int *a, b;
    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.
    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


  11. #11
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    Re: Professional placement of derefencing operator

    Quote 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.

  12. #12
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Professional placement of derefencing operator

    What about reference operators, Graham?

    Quote 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
    Code:
    int *a
    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.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  13. #13
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    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
  •  





Click Here to Expand Forum to Full Width

Featured