Indirection operator placement

Discussion in 'C++' started by Marty, Aug 21, 2006.

  1. Marty

    Marty Guest

    I am wondering what is the difference in the placement between the 2
    uses of the indirection operator? Or is there a difference?

    AcDbBlockTable *pBlockTable = NULL;

    AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();
     
    Marty, Aug 21, 2006
    #1
    1. Advertising

  2. Marty

    Guest

    There's no difference.

    Tolga Ceylan
     
    , Aug 21, 2006
    #2
    1. Advertising

  3. Marty posted:

    > I am wondering what is the difference in the placement between the 2
    > uses of the indirection operator? Or is there a difference?
    >
    > AcDbBlockTable *pBlockTable = NULL;
    >
    > AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();



    A C++ compiler ignores white space, so it sees all of the following
    identically:

    int*p=0;
    int *p=0;
    int* p = 0;
    int * p = 0 ;

    I think it's intuitive, however, to write it as follows:

    int *p = 0;

    , because it conveys that the asterisk is a part of the object's name --
    which is how things actually work:

    int *p, *q, r[5], (*s)[5], *t[5], Func(), *Func2(), (*Func3())[5];

    p is a pointer.
    q is a pointer.
    r is an array of 5.
    s is a pointer to an array of 5.
    t is an array of 5 pointers.
    Func is a function which returns an int.
    Func2 is a function which returns a pointer to an int.
    Func3 is a function which returns a pointer to an array of 5.

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 21, 2006
    #3
  4. Marty

    Howard Guest

    "Marty" <> wrote in message
    news:...
    >I am wondering what is the difference in the placement between the 2
    > uses of the indirection operator? Or is there a difference?
    >
    > AcDbBlockTable *pBlockTable = NULL;
    >
    > AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();
    >


    I assume you're talking about the "*" symbols in the code above? That's not
    an operator. It specifies that the object being declared is a pointer to
    the type specified on the left of the * symbol. The spacing is irrelevant.
    The following are all the same:

    int* pInt;
    int *pInt;
    int * pInt;
    int * pInt;

    All those line declare a variable named pInt whose type is "pointer-to-int".

    Some people prefer to put the * immediately after the type, others prefer to
    put it immediately before the identifier, and still others prefer to put
    spaces on both sides.

    The compiler doesn't care one way or the other. :)

    -Howard
     
    Howard, Aug 21, 2006
    #4
  5. Marty

    Marcus Kwok Guest

    Marty <> wrote:
    > I am wondering what is the difference in the placement between the 2
    > uses of the indirection operator? Or is there a difference?
    >
    > AcDbBlockTable *pBlockTable = NULL;
    >
    > AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();


    This is answered on Stroustrup's FAQ:
    http://www.research.att.com/~bs/bs_faq2.html#whitespace

    In short, they are the same. It's merely a matter of style which form
    you use. However, as noted on the referenced page, the confusion comes
    when someone tries to declare multiple pointers with a single
    declaration:

    int* p1; // pointer to int
    int *p2; // also pointer to int

    int* p3, p4; // p3 is a pointer to int, p4 is a regular int
    int* p5, *p6; // both p5 and p6 are pointers to int

    Since I prefer one declaration per line, this doesn't affect me, so I
    use the (int* p;) form instead of the (int *p;) form, for the reason
    given on the page: (int* p;) emphasizes the type (p is a pointer to
    int), versus (int *p;) which emphasizes the syntax (*p is an int).

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Aug 21, 2006
    #5
  6. Marty

    Marty Guest

    Thanks all!
    I am new to C++ (if you can't tell) but you confirmed what I thought
    was the answer.
    When I started looking through the example project, they used it as I
    had shown.
    It started to get me to double guess myself.
     
    Marty, Aug 21, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SL
    Replies:
    0
    Views:
    525
  2. Ioannis Vranos

    Placement operator new/delete question

    Ioannis Vranos, Oct 16, 2004, in forum: C++
    Replies:
    11
    Views:
    5,684
    Ioannis Vranos
    Oct 16, 2004
  3. Mark P
    Replies:
    6
    Views:
    811
    James Dennett
    Apr 27, 2005
  4. mangesh
    Replies:
    15
    Views:
    632
    mlimber
    Jun 30, 2006
  5. Paradigm
    Replies:
    3
    Views:
    344
    Jamie
    Jul 15, 2012
Loading...

Share This Page