Pointer help

B

BillGill

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
 
G

Gianni Mariani

BillGill said:
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.
 
R

red floyd

BillGill said:
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.
 
S

Salt_Peter

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
*/
 
E

Erik Wikström

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top