473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Pointer help

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
Jul 7 '08 #1
5 1383
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.

Jul 7 '08 #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?
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.
Jul 7 '08 #3
On Jul 6, 8:18 pm, BillGill <billne...@cox.netwrote:
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
*/
Jul 7 '08 #4
BillGill <bi*******@cox.netwrites:
>Is there some place, either a book
or a website that will take me by the hand and lead me carefully through
the maze?
Maybe
http://burks.bton.ac.uk/burks/langua...t/pointers.htm
or
http://www.codeproject.com/KB/cpp/pointers.aspx
Jul 7 '08 #5
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
Jul 7 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ali | last post by:
Hi, I'm trying to understand the reason for different output on the following codes Code1: #include <iostream.h> int main()
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
10
by: Simon | last post by:
I'm a js newbie trying to use some very simple js to call an ActiveX object's methods. I need to use a pointer to call an embedded ActiveX object's method to receive a number. As I understand it,...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
6
by: lithiumcat | last post by:
Hi, maybe you remember me, some time ago I asked about how to store an integer value into a void*, and I learned that doing pointer arithmetic yeilding a pointer outside of an object (except the...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.