Pointer help

Discussion in 'C++' started by BillGill, Jul 7, 2008.

  1. BillGill

    BillGill Guest

    Ok, I assume this has been asked many times, but I can't seem to come up with
    a good Google search to find it.

    I am trying to learn C++. Specifically I have Microsoft Visual C++ 2005
    Express Edition. I am trying to learn it from Ivor Horton's Beginning
    Visual C++ 2005.

    My problem comes when we get to pointers. I just cannot seem to wrap my
    mind around the complexities involved. Is there some place, either a book
    or a website that will take me by the hand and lead me carefully through
    the maze?

    I do understand a lot about programming. I know Visual Basic, but want to
    expand my abilities.

    Thanks a lot.

    Bill
    BillGill, Jul 7, 2008
    #1
    1. Advertising

  2. BillGill wrote:
    > Ok, I assume this has been asked many times, but I can't seem to come up
    > with
    > a good Google search to find it.
    >
    > I am trying to learn C++. Specifically I have Microsoft Visual C++ 2005
    > Express Edition. I am trying to learn it from Ivor Horton's Beginning
    > Visual C++ 2005.
    >
    > My problem comes when we get to pointers. I just cannot seem to wrap my
    > mind around the complexities involved. Is there some place, either a book
    > or a website that will take me by the hand and lead me carefully through
    > the maze?
    >
    > I do understand a lot about programming. I know Visual Basic, but want to
    > expand my abilities.


    Modern computers access memory through the use of "address". An address
    is a 32 or 64 bit number. Modern computer languages abstract these to
    the concept of a "pointer". The C and C++ languages represent memory
    addresses as pointers.

    e.g.

    char * x = "ABC";

    x in this case is the address of the 'A' part of the sequence characters
    'A', 'B', 'C', '\0'.

    Pointers can "point" to any type, e.g.

    int * p = new int[5];

    In this case, p is a pointer to an int which is the first "int" in an
    array of 5 ints.

    Pointer arithmetic becomes fun, if you added a int value to an address
    in the cpu, it would simply add the two integers, often not making much
    sense. The C and C++ pointers will do somthing more reasonable. It
    assumes that the object being pointed to is the atomic element so adding
    an in to a pointer will address the next elements.

    e.g.

    int * p = new int[5];

    (p+1) is a pointer to the second element in the int[5] array.

    You can also subtract two pointers - e.g.

    (p+2)-p evaluates to 2.

    Pointers are not very useful unless you can access the object being
    pointed to. There are a number of ways to do that.

    *p - this references the object being pointed to
    p[0] - this is the same as *(p+0) - which is *p
    p[N] - this is the same as *(p+N)

    note that 0[p] is also the same as *p ... somthing Kernigan should have
    avoided IMHO...

    There are a few more ways to deref a pointer.

    If you have a struct - e.g.

    struct A { int a; };

    A * p = new A;

    (*p).a - is one way of dereferencing the a element of the object pointed
    to by p.

    p->a does the same thing !!!

    In c++ you also have "pointer to member" which can be though of as the
    thing that references any element of a struct.

    e.g.


    struct A { int a; };

    int A::* m = &A::a;
    A * p = new A;

    p->*m referernces p->a...

    There are some very interesting properties of member pointers but that
    gets pretty hairy.

    Hopefully this will help you grok the book you're reading a little better.
    Gianni Mariani, Jul 7, 2008
    #2
    1. Advertising

  3. BillGill

    red floyd Guest

    BillGill wrote:
    > Ok, I assume this has been asked many times, but I can't seem to come up
    > with
    > a good Google search to find it.
    >
    > I am trying to learn C++. Specifically I have Microsoft Visual C++ 2005
    > Express Edition. I am trying to learn it from Ivor Horton's Beginning
    > Visual C++ 2005.
    >
    > My problem comes when we get to pointers. I just cannot seem to wrap my
    > mind around the complexities involved. Is there some place, either a book
    > or a website that will take me by the hand and lead me carefully through
    > the maze?
    >


    The most clear and lucid explanation of pointers I have ever seen was in
    Cooper & Clancy's "Oh Pascal!". I think it may be out of print, but
    it's worth it for the chapter on pointers alone, even if it's a Pascal
    book, and the syntax is different.
    red floyd, Jul 7, 2008
    #3
  4. BillGill

    Salt_Peter Guest

    On Jul 6, 8:18 pm, BillGill <> wrote:
    > Ok, I assume this has been asked many times, but I can't seem to come up with
    > a good Google search to find it.
    >
    > I am trying to learn C++. Specifically I have Microsoft Visual C++ 2005
    > Express Edition. I am trying to learn it from Ivor Horton's Beginning
    > Visual C++ 2005.
    >
    > My problem comes when we get to pointers. I just cannot seem to wrap my
    > mind around the complexities involved. Is there some place, either a book
    > or a website that will take me by the hand and lead me carefully through
    > the maze?
    >
    > I do understand a lot about programming. I know Visual Basic, but want to
    > expand my abilities.
    >
    > Thanks a lot.
    >
    > Bill


    declaring a pointer does NOT invoke a constructor
    its just an address that _could_ point to a valid, initialized object
    nobody cares what the exact value of the address stored is in the
    pointer,
    as long as it points to a valid object

    In other words, the following generates a seg fault

    #include <iostream>

    class N
    {
    int n;
    public:
    N(int i = 0) : n(i)
    {
    std::cout << "N()\n";
    }
    int get() const { return n; }
    };

    int main()
    {
    N* ptr; // points to garbage
    // N instance;
    // ptr = &instance;
    std::cout << ptr->get() << std::endl;
    }

    /*
    .... Segmentation Fault ...
    */

    /* with commented lines infused... we have success
    N()
    0
    */
    Salt_Peter, Jul 7, 2008
    #4
  5. BillGill

    Tim Love Guest

    Tim Love, Jul 7, 2008
    #5
  6. On 2008-07-07 02:18, BillGill wrote:
    > Ok, I assume this has been asked many times, but I can't seem to come up with
    > a good Google search to find it.
    >
    > I am trying to learn C++. Specifically I have Microsoft Visual C++ 2005
    > Express Edition. I am trying to learn it from Ivor Horton's Beginning
    > Visual C++ 2005.


    I'd like to point out that while that book is probably a good book, it
    does teach you Windows programming, which is not quite the same as C++.
    If all you want is to write programs that runs under Windows that is
    probably fine, but if you want to write programs that can run on other
    platforms too, or learn how to write good C++ you probably need some
    other book. If you want advice on some good books just search this news-
    group and you will find lots of suggestions.

    --
    Erik Wikström
    Erik Wikström, Jul 7, 2008
    #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. Replies:
    10
    Views:
    676
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    827
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,234
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    626
  5. , India

    pointer to an array vs pointer to pointer

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

Share This Page