473,776 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1401
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
2346
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
9960
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 must be an object instead of
10
4394
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, js is typeless so how can I get a variable to be a pointer type? Passing var i as the parameter gets an undefined value in return. Thanks IA, Simon
5
6062
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 hits the right and left keys to move this insertion point (cursor) Here is the problem:
7
5195
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 this. Ok... here goes.... I have a function that passes a pointer to a string to another function. For example: int FunctionA ()
27
8977
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 advance. Erik
12
3886
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 looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
26
3064
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 read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
6
2324
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 one- after-last thingy) is undefined behaviour. Actually I was trying to associate a function pointer with a key, through an AVL tree that managed void* data. Function pointers can't be stored in void* (that is, the standard does not garantee...
20
2244
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 think my way through what I want to do now. I don't know why - I'm fine with most other aspects of the language, but my brain goes numb when I'm reading about function pointers! I would like to have an array of structures, something like
0
9627
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9462
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10060
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8951
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5367
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4030
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3621
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2859
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.