pointer problem in C++

Discussion in 'C++' started by Bob Keith, Jul 1, 2003.

  1. Bob Keith

    Bob Keith Guest

    Hello, all:

    I am a beginner of C++, and feel confusing about the differences of
    the following terms regarding pointer.

    int *a
    int* a
    int** a
    int*& a
    int *&a
    int &a

    Many thanks.
     
    Bob Keith, Jul 1, 2003
    #1
    1. Advertising

  2. "Bob Keith" <> wrote in message
    news:...
    > Hello, all:
    >
    > I am a beginner of C++, and feel confusing about the differences of
    > the following terms regarding pointer.
    >
    > int *a
    > int* a


    Both are the same. 'a' is a pointer to an int.

    > int** a


    'a' is a pointer to a pointer to an int.

    > int*& a
    > int *&a


    Again, those two are the same. 'a' is a reference to a pointer to an
    int.

    > int &a


    And here 'a' is just a reference to an int.

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)
     
    Jakob Bieling, Jul 1, 2003
    #2
    1. Advertising

  3. Bob Keith

    Howard Guest

    "Bob Keith" <> wrote in message
    news:...
    > Hello, all:
    >
    > I am a beginner of C++, and feel confusing about the differences of
    > the following terms regarding pointer.
    >
    > int *a
    > int* a
    > int** a
    > int*& a
    > int *&a
    > int &a
    >
    > Many thanks.


    Didn't you read the replies to your post last week?
     
    Howard, Jul 1, 2003
    #3
  4. (Bob Keith) wrote in message news:<>...
    > int *a
    > int* a
    > int** a
    > int*& a
    > int *&a
    > int &a


    Trying to read between the lines here, I'm guessing there are two
    things that are confusing you.

    First, to knock off the easy one, let's talk about whitespace. In
    general, where whitespace goes is irrelevant (although not always).
    So there is no difference between "int *a" and "int* a". Some people
    prefer the former because of the rules of declaring multiples
    variables (and int* x,y declares x as a pointer to an integer and y as
    an integer, but the placement of the * implies otherwise) while others
    prefer the latter because the * modifies the type. Just pick one.
    When I critique people, I don't care which one they use as long as
    they are consistent.

    The second issue that looks to be confusing you is references and
    pointers. That is normal because the two are quite similiar and all
    the implementations I know (which admittedly is not huge) implement
    references via pointers.

    Both pointers and references can "reference" another variable. But
    they do have some differences.

    1) Pointers use 'special' syntax while references use standard object
    syntax. To access a field of a pointer, you use the -> operator you
    use the . operator to access a field of a reference.

    2) References must always be bound to a variable while pointers can
    legally point to nothing.

    My general recommendation is to start by only using references as
    formal parameters to a function. Don't bother trying to fully
    understand references until that feels comfortable.

    samuel
     
    Ron Samuel Klatchko, Jul 1, 2003
    #4
  5. Bob Keith

    David White Guest

    Ron Samuel Klatchko <> wrote in message
    news:...
    > (Bob Keith) wrote in message

    news:<>...
    > > int *a
    > > int* a
    > > int** a
    > > int*& a
    > > int *&a
    > > int &a

    >
    > Trying to read between the lines here, I'm guessing there are two
    > things that are confusing you.
    >
    > First, to knock off the easy one, let's talk about whitespace. In
    > general, where whitespace goes is irrelevant (although not always).
    > So there is no difference between "int *a" and "int* a". Some people
    > prefer the former because of the rules of declaring multiples
    > variables (and int* x,y declares x as a pointer to an integer and y as
    > an integer, but the placement of the * implies otherwise)


    Rather than the rules of declaring multiple variables, it is likely to be
    because of the rules of the language. To the compiler, the * is bound to the
    identifier, not the type. The "rules of declaring of multiple variables" are
    simply a consequence of that. If you write "int* x,y" the compiler sees the
    type as 'int' and the ints being declared as *x and y.

    To the OP, the declaration rules are designed to mimic expressions.

    int k;
    This says that where 'k' appears in an expression, its type is 'int', e.g.,
    k = 1;
    k = k + 3;

    int *pk = &k;
    This says that where *pk appears in an expression, its type is also 'int'.
    The * is the dereference-pointer operator. Therefore 'pk' is a pointer to an
    int, and *pk is the int that 'pk' points to, in this case 'k'.

    *pk = *pk + 1;

    Since *pk is an int, this expression takes the int at *pk, adds 1 to it, and
    stores it back to *pk.

    Some people think it's silly to think of "int *pk" as "declare *pk to be of
    type int", even though that's what it was designed to mean. Those people
    prefer to think "declare pk to be of type pointer-to-int". That's why they
    write "int* pk".

    Note that it is only an accident of history that the * was placed on the
    left of the identifier and not the right, which enables you to move the *
    across the whitespace to the type name. There are many other declarations in
    which you can't do that, e.g.,

    int a[10];
    Array of 10 ints. (You can't have "int[10] a;")

    int (*pf)();
    Pointer to a function returning an int (i.e., where (*pf)() appears in an
    expression its type is 'int')

    int (*pa)[10];
    Pointer to an array of 10 ints (i.e., where (*pa) appears in an
    expression, its type is 'int')

    Make your own choice.

    David
     
    David White, Jul 2, 2003
    #5
  6. "Bob Keith" <> wrote in message
    news:...
    > Hello, all:
    >
    > I am a beginner of C++, and feel confusing about the differences of
    > the following terms regarding pointer.


    Perhaps you would be less confused if you'd read the answer I gave to your
    post less than a week ago which bore a remarkable resemblance to this one.
    Please read the replies you get this time.

    > int *a


    a is a pointer to an int

    > int* a


    a is a pointer to an int

    > int** a


    a is a pointer to a pointer to an int

    > int*& a


    a is a reference to a pointer to an int

    > int *&a


    a is a reference to a pointer to an int

    > int &a


    a is a reference to an int

    > Many thanks.


    You're welcome... :) And I recommend the following books, since asking for
    book recommendations was implicit in your post:

    The C++ Programming Language (Stroustrup)
    C++ Primer (Lippmann and Lajoie)
    Accelerated C++ (Koenig and Moo)

    HTH,

    Stuart.
     
    Stuart Golodetz, Jul 2, 2003
    #6
  7. "David White" <> wrote in message
    news:c6pMa.8261$...
    > Ron Samuel Klatchko <> wrote in message
    > news:...
    > > (Bob Keith) wrote in message

    > news:<>...
    > > > int *a
    > > > int* a
    > > > int** a
    > > > int*& a
    > > > int *&a
    > > > int &a

    > >
    > > Trying to read between the lines here, I'm guessing there are two
    > > things that are confusing you.
    > >
    > > First, to knock off the easy one, let's talk about whitespace. In
    > > general, where whitespace goes is irrelevant (although not always).
    > > So there is no difference between "int *a" and "int* a". Some people
    > > prefer the former because of the rules of declaring multiples
    > > variables (and int* x,y declares x as a pointer to an integer and y as
    > > an integer, but the placement of the * implies otherwise)

    >
    > Rather than the rules of declaring multiple variables, it is likely to be
    > because of the rules of the language. To the compiler, the * is bound to

    the
    > identifier, not the type. The "rules of declaring of multiple variables"

    are
    > simply a consequence of that. If you write "int* x,y" the compiler sees

    the
    > type as 'int' and the ints being declared as *x and y.
    >
    > To the OP, the declaration rules are designed to mimic expressions.
    >
    > int k;
    > This says that where 'k' appears in an expression, its type is 'int',

    e.g.,
    > k = 1;
    > k = k + 3;
    >
    > int *pk = &k;
    > This says that where *pk appears in an expression, its type is also 'int'.
    > The * is the dereference-pointer operator. Therefore 'pk' is a pointer to

    an
    > int, and *pk is the int that 'pk' points to, in this case 'k'.
    >
    > *pk = *pk + 1;
    >
    > Since *pk is an int, this expression takes the int at *pk, adds 1 to it,

    and
    > stores it back to *pk.
    >
    > Some people think it's silly to think of "int *pk" as "declare *pk to be

    of
    > type int", even though that's what it was designed to mean. Those people
    > prefer to think "declare pk to be of type pointer-to-int". That's why they
    > write "int* pk".
    >
    > Note that it is only an accident of history that the * was placed on the
    > left of the identifier and not the right, which enables you to move the *
    > across the whitespace to the type name. There are many other declarations

    in
    > which you can't do that, e.g.,
    >
    > int a[10];
    > Array of 10 ints. (You can't have "int[10] a;")
    >
    > int (*pf)();
    > Pointer to a function returning an int (i.e., where (*pf)() appears in an
    > expression its type is 'int')
    >
    > int (*pa)[10];
    > Pointer to an array of 10 ints (i.e., where (*pa) appears in an
    > expression, its type is 'int')
    >
    > Make your own choice.


    http://www.research.att.com/~bs/bs_faq2.html#whitespace

    regards
    --
    jb

    (replace y with x if you want to reply by e-mail)
     
    Jakob Bieling, Jul 2, 2003
    #7
  8. Bob Keith

    David White Guest

    "Jakob Bieling" <> wrote in message
    news:bdu4uk$isq$00$-online.com...
    >
    > http://www.research.att.com/~bs/bs_faq2.html#whitespace


    Thanks. Interesting, but I don't entirely agree with Stroustrup. I have
    serious doubts that C and C++ programmers really see pointer declarations
    differently. I usually think of 'p' in "int *p" as a "pointer to int" as
    well, and I came to C++ from C. The only time I really think of it as "*p is
    an int" is where *p appears in an expression and when I explain the syntax
    rules in places like this. But that has no influence on how I write
    declarations, simply because to write "int* p" is a special case. You simply
    can't declare a pointer to a function, for example, in such a way that you
    can have the type information in one place and the identifier somewhere
    else, with whitespace in between. I hate special cases. I prefer to follow
    the rules that the compiler follows, not fight against them and be
    inconsistent in the process.

    I also suspect that having an "int *p" camp and an "int* p" camp confuses
    the hell out of people trying to learn the language. I don't believe that
    you explain the declaration syntax rules well enough for a student to
    understand any declaration without the "int* p" style appearing to
    contradict the words used to explain the rules.

    David
     
    David White, Jul 2, 2003
    #8
  9. Bob Keith

    Gavin Deane Guest

    "David White" <no.email@provided> wrote in message news:<uhyMa.8300$>...
    > "Jakob Bieling" <> wrote in message
    > news:bdu4uk$isq$00$-online.com...
    > >
    > > http://www.research.att.com/~bs/bs_faq2.html#whitespace

    >
    > Thanks. Interesting, but I don't entirely agree with Stroustrup. I have
    > serious doubts that C and C++ programmers really see pointer declarations
    > differently. I usually think of 'p' in "int *p" as a "pointer to int" as
    > well, and I came to C++ from C. The only time I really think of it as "*p is
    > an int" is where *p appears in an expression and when I explain the syntax
    > rules in places like this. But that has no influence on how I write
    > declarations, simply because to write "int* p" is a special case. You simply
    > can't declare a pointer to a function, for example, in such a way that you
    > can have the type information in one place and the identifier somewhere
    > else, with whitespace in between. I hate special cases. I prefer to follow
    > the rules that the compiler follows, not fight against them and be
    > inconsistent in the process.
    >
    > I also suspect that having an "int *p" camp and an "int* p" camp confuses
    > the hell out of people trying to learn the language. I don't believe that
    > you explain the declaration syntax rules well enough for a student to
    > understand any declaration without the "int* p" style appearing to
    > contradict the words used to explain the rules.


    "int *p" confused the hell out of me until I realised I could write it
    as "int* p". Just the way I think about things I suppose. Admittedly,
    I learnt these things as they came up, rather than someone formally
    explaining the full set of declaration syntax rules in one go.

    I understand your point about "int* p" being a special case that
    cannot be applied to function pointers. But when I later encountered
    function pointers, I found that there was so much more syntax going on
    in the declarations that the inconsistency in the position of the *
    was the least of my worries. I could cope with that, it was all the
    parentheses all over the place that bothered me :)

    I would still be much happier if I could completely separate type from
    variable name, something like

    void (*)(int) pf;

    instead of

    void (*pf)(int);

    At the end of the article, Stroustrup says "Whenever something can be
    done in two ways, someone will be confused. Whenever something is a
    matter of taste, discussions can drag on forever." Sounds to me like a
    convincing argument for not having things that can be done two ways as
    a matter of taste.

    GJD
     
    Gavin Deane, Jul 2, 2003
    #9
    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. Replies:
    10
    Views:
    704
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    844
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,269
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    639
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    457
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page