CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jul 2005
    Posts
    11

    Best Way To Declare Pointers

    Once Again, My Questions is so simple that it's even in the FAQ. My question is, is there a difference between these three statements?

    int* i
    int * i
    int *i

    I've seen all three around, and from what I've read I think they're all saying the same thing, but I want to be sure.

    Eric

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Best Way To Declare Pointers

    Same thing. The compiler ignores whitespace ( but not in strings, mind you ). So they all mean the same.

    Note: if you do this:
    int* p,k;
    while it appears that both p and k are pointers, it is wrong.
    Only p is a pointer, but k is a int

    So, people generally stick the * close to the variable and not type. Something like this:
    int *p,*k;

  3. #3
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Best Way To Declare Pointers

    Quote Originally Posted by Carbaholic
    Once Again, My Questions is so simple that it's even in the FAQ. My question is, is there a difference between these three statements?

    int* i
    int * i
    int *i

    I've seen all three around, and from what I've read I think they're all saying the same thing, but I want to be sure.
    Syntactically, no difference at all. They all produce exactly the same code. However, there is a decade-old debate of whether it's clearer to write "int* i" or "int *i".
    Personally, I prefer "int* i", since I read this as "declare a variable of the type int* (pointer to an int) with the name i" - and hence, the * is part of the type and should stand next to it. The problem with this notion is the short-hand way of variable declaration in C/C++ which lets you declare a series of variables of the same type, separated by commas, and without repeating the type:
    Code:
    int a, b, c; // Declares three ints
    The problem arises when you write the following:
    Code:
    int* pA, pB, pC;
    Although this suggests that three pointers are declared, actually, this declares just one pointer (pA) and two ints (pB and pC), since the * is syntactically associated to the variable, not the type. To declare three pointers, you would rather write:
    Code:
    int *pA, *pB, *pC; // Declares three int pointers
    That's why many experienced people prefer putting the pointer operator next to the variable name to clarify this.
    However, my personal preference is to put it next to the type, and to completely avoid declaring multiple variables separated by commas (especially if they have mixed levels of indirection). I always write it this way:
    Code:
    int* pA;
    int* pB;
    int* pC;
    Last edited by gstercken; July 22nd, 2005 at 05:51 PM.

  4. #4
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Best Way To Declare Pointers

    Quote Originally Posted by kirants
    So, people generally stick the * close to the variable and not type.
    Generally?

  5. #5
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Best Way To Declare Pointers

    Woops !! We pretty much echoed the same thing
    Well, maybe not generally ... I don't use the int *p1,*p2,*p3 style too..
    So, maybe , occasionally is a better word

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Best Way To Declare Pointers

    If we talk about personal preferences, than I always put the * to the Type, when I declare a pointer to Type, and in case there is need for more than just one pointer I declare them separatelly:
    Code:
    Type* pointer1;
    Type* pointer2;
    I consider this one to be the most readable form.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Aug 1999
    Posts
    586

    Re: Best Way To Declare Pointers

    Quote Originally Posted by gstercken
    ... and hence, the * is part of the type and should stand next to it.
    Actually, while this is all a religious issue anyway, in the language itself the "*" actually binds with the variable name. Dan Saks (former secretary of the ANSI and ISO C++ committee and all-round C++ guru) once wrote a multi-part article on declarators in the C++ Users Journal and advocated joining the "*" with the variable name. Funny thing is that Stroustrup himself doesn't do it that way in all of his writings. I personally agree with Dan on this one and have done it that way for 20+ years myself (previously in C). My two cents

  8. #8
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729

    Re: Best Way To Declare Pointers

    just to throw my 2 cents in.... i personally advocate the use of

    int *p1, *p2;

    style syntax for the previously mentioned reasons.



    ultimately this is a cosmetic issue though. do what makes most sense to you (and anyone else who might see your code)

  9. #9
    Join Date
    Jul 2005
    Posts
    11

    Re: Best Way To Declare Pointers

    Thank You for your responses, I am now, officially, much less confused

    Eric

  10. #10
    Join Date
    Jul 2003
    Posts
    731

    Re: Best Way To Declare Pointers

    At last, I have one thing in common with Cilu and gstercken that I declare it as int* myVariable too ..oh wait eveyone does it like that?
    Good Answers, Good Points.

  11. #11
    Join Date
    Jun 2005
    Posts
    1,255

    Re: Best Way To Declare Pointers

    Let's see what the authors of the C language are doing.
    For instance, B.W. Kerningham and D.M. Ritchie illustrate their discourse about pointers with
    Code:
    swap (px, py)
    int *px, *py;
    {
       int temp;
    
       temp = *px;
       *px = *py;
       *py = temp;
    }
    Well, they use "*px" like I do (but I don't use the odd and old syntax on two lines for the header of a function).

    Personnally I prefer to be consistent and always glue the star next to the variable.
    Otherwise you write "int* px" and "temp = *px;" (and not temp = * px; (not to mention the case of the multiplication when you write m = a * *px; instead of m = a * * p; )).

  12. #12
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Best Way To Declare Pointers

    Quote Originally Posted by olivthill
    Well, they use "*px" like I do (but I don't use the odd and old syntax on two lines for the header of a function).
    Of course not - that way of declaring function parameters is deprecated since function prototypes have been introduced...

    Quote Originally Posted by olivthill
    Otherwise you write "int* px" and "temp = *px;" (and not temp = * px).
    But that are two completely different uses of the '*': In the first case, you declare a pointer variable, in the second case, you dereference it.

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