Question about C++/Pointers

Ichinisan

Lifer
Oct 9, 2002
28,298
1,235
136
I am learning C++ and I'm confused about the purpose of a pointer variable. To my understanding, you can pass any variable to a function by reference (address) or by value.

(Warning: Corny pun)
What is the point of creating a pointer?
 

reicherb

Platinum Member
Nov 22, 2000
2,122
0
0
It's been quite a while since I've had C so I'm not going to try and define a pointer, but imaging this. You write a program to access memory address xxxx:xxxx and someone else writes a program to use the same address. They couldn't be used at the same time. This is one reason pointers are used.

Someone else can give a much better technical reason I'm sure.
 

Ichinisan

Lifer
Oct 9, 2002
28,298
1,235
136
You are talking about accessing an address allocated for storage by another process?
The examples in my book just demonstrate creating normal variables with pointers, and accessing them through pointers.

All of my books demonstrate passing a pointer to a function, but I see no reason to do it this way instead of passing by reference.
 

manly

Lifer
Jan 25, 2000
13,093
3,858
136
Pointers are a fundamental concept in computer programs. Since you didn't ask what pointers are or are good for, I won't try to give a good explanation of that. You can certainly Google or refer to a good text for that topic.

However, specifically regarding C++ references and pointers, sure both can serve the same purpose, with slightly different syntax. Pointers and the notation were taken from C, whereas references were a new feature in C++.

In The C++ Programming Language, Bjarne Stroustrup, the language's creator, discourages using references as modifyable arguments because such functions can be more subtle/confusing (sections 5.5 and 7.2). Using pointers to modify function arguments is explicit and clear in comparison. He advises that reference parameters to functions should be declared const so that such objects are not mistakenly modified in the calling context.

I'm not a C++ developer so I don't know what standard practice is, but I think the reasoning here is solid: using references for modifyable function arguments results in subtler code and more potential for error.
 

Oogle

Member
Feb 18, 2002
63
0
0
What is the point of creating a pointer?
One of the reason for using a pointer is for memory allocation. There's no other way in C++ to do something simple like this without using a pointer...

int *i = new int;
int *i = malloc(sizeof(int));
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
An easy way to understand pointers is to think 'big'.

A house is a physical location on a plot of land.
the Address is a reference to that location.

When you address a letter, the post office takes it to the location and puts it in a mail box. (via address)
The mailbox is a pointer to the aqctual house. Most postal deliveries do not take the letter to the actual house.
 

PCHPlayer

Golden Member
Oct 9, 2001
1,053
0
0
There are many good replies above. Just thought I'd add one more.
Pointers are really inherited from C. I rarely use them anymore and almost never use them in method arguments. I just hate checking for null in my methods (like I always did in all my old C programs that took pointer arguments).
Three reasons I use pointers:
1. If the value of the pointer can be null.
2. Using an STL container with classes that large/complex enough that I don't want to take a performance hit copying the object into the container controlled object (rare)
3. Using an STL container to pass a object between threads (e.g. std::Queue<MyClass *> interThreadQueue;)