Poll: How do you declare c/c++/c# style pointers?

Discussion in 'Programming' started by irishScott, Dec 4, 2012.

?

What syntax do you use?

  1. int* pointer

  2. int * pointer

  3. int *pointer

  4. More than one

Multiple votes are allowed.
Results are only viewable after voting.
  1. irishScott

    irishScott Lifer

    Joined:
    Oct 10, 2006
    Messages:
    21,570
    Likes Received:
    1
    And what's your reasoning? Just curious. Everywhere I go I see people using "int *p;" or occasionally "int * p", I seem to be the only one who uses "int* p". It makes sense to me because the type of variable in question is an "int pointer". The asterisk is essentially part of the type-name. Can read the other variations fine of course, but when I do it just mildly spreads out the reading comprehension.

    In my mind:
    int* sam = "int pointer sam"
    int * sam = "int.. pointer sam
    int *sam = "int pointersam"
     
  2. Absolution75

    Absolution75 Senior member

    Joined:
    Dec 3, 2007
    Messages:
    981
    Likes Received:
    0
    What are the types of the following?

    int* i, j, k;


    Thus, i do int *p;
     
    #2 Absolution75, Dec 4, 2012
    Last edited: Dec 4, 2012
  3. Pia

    Pia Golden Member

    Joined:
    Feb 28, 2008
    Messages:
    1,563
    Likes Received:
    0
    I wish the asterisk was part of the typename, but it's not. So we get this problem:
    int* p, p2; // oops, p2 is int
    which would be easier to spot and avoid if you always used "int *x":
    int *p, *p2;
    Anyway, I occasionally use both "int* foo" and "int *foo", but not "int * foo". That just looks stupid somehow.
    I think generally the best is to use "int* foo" and just not declare multiple pointers at the same time.
     
  4. EagleKeeper

    EagleKeeper Discussion Club Moderator<br>Elite Member
    Staff Member

    Joined:
    Oct 30, 2000
    Messages:
    42,600
    Likes Received:
    1
    Prefer:
    int *p;

    Doing so, removes any potential chance of what others showed (confusion)
    int *p, p1;

    Also, I prefer to not mix and match
    int *p;
    int p1;
     
  5. brandonb

    brandonb Diamond Member

    Joined:
    Oct 17, 2006
    Messages:
    3,731
    Likes Received:
    1
    I personally use int *sam.

    It just seems more natural to me that it's a pointer. Much like I would also prefix the & (addressof) directly in front of the variable.

    Code:
    int *sam;
    int sam2;
    
    sam = &sam2;
    
     
  6. Crusty

    Crusty Lifer

    Joined:
    Sep 30, 2001
    Messages:
    12,688
    Likes Received:
    1
    Pretty much this, one declaration per line eliminates all of the ambiguities as well as improves readability.
     
  7. veri745

    veri745 Golden Member

    Joined:
    Oct 11, 2007
    Messages:
    1,154
    Likes Received:
    2
    I use
    int* p;

    If you're not going to mix and match multiple declarations on a line, what difference does it make?

    I never declare multiple variables on a single line.
     
  8. TecHNooB

    TecHNooB Diamond Member

    Joined:
    Sep 10, 2005
    Messages:
    7,454
    Likes Received:
    0
    i usually do int * p or int *p. soemtimes it depends on what surrounding code looks like
     
  9. Markbnj

    Markbnj Elite Member <br>Moderator Emeritus
    Moderator

    Joined:
    Sep 16, 2005
    Messages:
    15,688
    Likes Received:
    8
    It's one of the unfortunate things about C grammar. Pretty much everyone would prefer int*, and almost nobody would actually write int* a, b, c anymore, I hope, but because it's allowed most people learn to write int *p.
     
  10. irishScott

    irishScott Lifer

    Joined:
    Oct 10, 2006
    Messages:
    21,570
    Likes Received:
    1
    Same. I've also never encountered a situation where it was advantageous to declare multiple pointers on the same line as opposed to just giving each it's own line/unique name.
     
  11. EagleKeeper

    EagleKeeper Discussion Club Moderator<br>Elite Member
    Staff Member

    Joined:
    Oct 30, 2000
    Messages:
    42,600
    Likes Received:
    1
    I have seen many "hotshots" out of school stack them up on a line.

    either they think that extra lines are going to cost them speed, memory, storage or they have the attitude that commenting and readability are not for the prima donna.
     
  12. clamum

    clamum Lifer

    Joined:
    Feb 13, 2003
    Messages:
    24,085
    Likes Received:
    41
    I haven't written C++ since a course I took on it in college (so, 7-8 years ago?); if I did now I would write "int *p". Even so, I would not declare multiple variables on a single line.
     
  13. mosco

    mosco Senior member

    Joined:
    Sep 24, 2002
    Messages:
    940
    Likes Received:
    1
    Does Objective-C count?

    NSString *name = @"Jim";
     
  14. Broheim

    Broheim Diamond Member

    Joined:
    Feb 17, 2011
    Messages:
    4,587
    Likes Received:
    2
    I do it in Java if it's something really generic that I need a bunch of.
     
  15. iCyborg

    iCyborg Golden Member

    Joined:
    Aug 8, 2008
    Messages:
    1,173
    Likes Received:
    2
    I use int*.
    It's part of the type name, not variable name, so I like it better this way. But it's more of a habit than anything else. I never declare pointers and nonpointers in the same line anyway.

    I also prefer 'int* const' to 'int *const'.
     
  16. Red Squirrel

    Red Squirrel Lifer

    Joined:
    May 24, 2003
    Messages:
    40,784
    Likes Received:
    1,238
    I usually do int * p; and I never do more than one per line.

    If I'm writing some parsing code or something where I have a bunch of temp variables that get reused, then I might declare on the same line. ex:

    int tmpint1, tmpint2, tmpint3;
    char tmpch1, tmpch2, tmpch3;
    string tmpstr1, tmpstr2;
    etc

    But variables that serve different/dedicated purposes get their own line.
     
  17. degibson

    degibson Golden Member

    Joined:
    Mar 21, 2008
    Messages:
    1,389
    Likes Received:
    0
    This.
     
  18. Pia

    Pia Golden Member

    Joined:
    Feb 28, 2008
    Messages:
    1,563
    Likes Received:
    0
    Huh? You have to give them unique names even if you put them on the same line.

    I don't think declaring multiple variables per line is bad as such. If your style is good, you just rarely have multiple variables to declare at the same time, because you are declaring everything just before you need it, and also initializing it right off the bat which causes you to not want to chain more declarations afterwards. And with C++, you are especially unlikely to declare several naked pointers because you don't have many of them to go around overall.