What can pointers do that other things can't do?

M

Michael Bell

I am trying to put my learning effort into the most useful things.
What do pointers do that other things can't?

Michael Bell

--
 
T

Tomasz bla Fortuna

Dnia Sun, 02 Dec 2007 14:07:10 GMT
Michael Bell said:
I am trying to put my learning effort into the most useful things.
What do pointers do that other things can't?
Well, they can... point? I guess you should get some C-related book.
Without pointers in C you won't be even able to operate on text
(strings).

Have you read c-faq?

best regards,
 
E

Erik Wikström

I am trying to put my learning effort into the most useful things.
What do pointers do that other things can't?

I suppose that with other things you mean references (anything else will
be too much like comparing apples to oranges, comparing pointers with
references is more like apples and pears: at least there are some
similarities). There are a number of differences:

Pointers can be null-pointers (pointing to nothing) whereas a reference
always (should) referees to something. You can always test if a pointer
is a null-pointer but you can not tell if a reference refers to a valid
object.

Pointers are "objects" in their own right and you can create a pointer
to a pointer (taking the address of a pointer) while you can never
create a pointer to a reference or a reference to a reference.

You can change what a pointer points to, you can never change what a
reference refers to.

You can perform arithmetic operations on a pointer, i.e. if p is a
pointer to the first element of an array then (p + 5) refers to the 6th
element of the same array.

From the programs point of view there is no difference between a
reference to an object and the object itself, you can treat the
reference (perform the same operations, use as arguments, etc.) in the
same way as the object. A pointer on the other hand is just an object
that points to some other object and operations performed on the pointer
does not affect the object it points to.
 
S

Salt_Peter

I am trying to put my learning effort into the most useful things.
What do pointers do that other things can't?

Michael Bell

--

A pointer is an address to nothing_at_all until it actually points to
something valid.

int* p;

p is a pointer with a residual value (garbage address). Nobody cares
what value p might hold now.
Since the statement does not invoke a (primitive) constructor, its an
invalid address to nothing.
The danger in the statement above is pretending that the pointer is
valid (a dangling pointer).

int n(99);
int* p = &n;

Ah, now a ctor was invoked. The pointer is now initialized with a
variable's address. There is no distinction between n or *p at this
point. Whats the difference you ask? Pointer p could be modified to
point to yet another object.

int m(77);
p = &m; // reseated pointer

Which brings us to the topic of references. You can't reseat a
reference. Its permanently bound to its target.
The syntax isn't the same at all.

int j;
int& ref = j; // ref is permanently bound
// ref = m; // error
ref = 55; // ok, j is now set to 55

___
All this to state:
a) Use a reference instead of a pointer whenever you can. Its the best
insecticide i know.
b) If you must use a pointer, always initialize it (if you can't give
it a valid address yet - then null it)
 
J

Juha Nieminen

Michael said:
I am trying to put my learning effort into the most useful things.
What do pointers do that other things can't?

Well, for example creating a linked list (with all kinds of
operations, such as removing an element in constant time) would be quite
difficult without pointers. (Perhaps not impossible, but certainly more
awkward.)
 
J

James Fang

I am trying to put my learning effort into the most useful things.
What do pointers do that other things can't?

Michael Bell

--

CPP Pointer can be used to do anything, as in the assembly level most
of data fetching opperations are done with "pointers".

The only thing constraints pointer's ability is the memory protection
mechanism provided by the processor, which is managed by the operating
system. You can only read/write the address area which is allowed by
the operating system.
 
M

Michael Bell

In message <[email protected]>
Well, for example creating a linked list (with all kinds of
operations, such as removing an element in constant time) would be quite
difficult without pointers. (Perhaps not impossible, but certainly more
awkward.)

Thank you for this. It has told me something of value: that it is
worth my time and effort to learn about pointers.

Michael Bell


--
 
M

Michael Bell

In message <[email protected]
ps.com>
A pointer is an address to nothing_at_all until it actually points to
something valid.
p is a pointer with a residual value (garbage address). Nobody cares
what value p might hold now.
Since the statement does not invoke a (primitive) constructor, its an
invalid address to nothing.
The danger in the statement above is pretending that the pointer is
valid (a dangling pointer).
int n(99);
int* p = &n;
Ah, now a ctor was invoked. The pointer is now initialized with a
variable's address. There is no distinction between n or *p at this
point. Whats the difference you ask? Pointer p could be modified to
point to yet another object.
int m(77);
p = &m; // reseated pointer
Which brings us to the topic of references. You can't reseat a
reference. Its permanently bound to its target.
The syntax isn't the same at all.
int j;
int& ref = j; // ref is permanently bound
// ref = m; // error
ref = 55; // ok, j is now set to 55
___
All this to state:
a) Use a reference instead of a pointer whenever you can. Its the best
insecticide i know.
b) If you must use a pointer, always initialize it (if you can't give
it a valid address yet - then null it)

This is jargon I don't know. Does "reseat" mean to point a pointer at
some other address? That seems to the meaning of
int m(77);
p = &m; // reseated pointer


What exactly is going on here?
j has been declared. No problems about that!
int& ref = j; // ref is permanently bound What has happened here?
// ref = m; // error
ref = 55; // ok, j is now set to 55

Sorry, I just don't understand.

Michael Bell



--
 
M

Matthias Buelow

Michael said:
Thank you for this. It has told me something of value: that it is
worth my time and effort to learn about pointers.

If it's worth wasting people's time on Usenet, it's certainly worth
learning about yourself.
 
S

Salt_Peter

In message <[email protected]
ps.com>






This is jargon I don't know. Does "reseat" mean to point a pointer at
some other address? That seems to the meaning of

reseat means to give it another address of course
What exactly is going on here?

the Variable m is initialized to 77 and that variable exists somewhere
in memory (where exactly the programmer doesn't care). That location
is its address which is what p is storing for you.
j has been declared. No problems about that!> int& ref = j; // ref is permanently bound

What has happened here?

Thats not how you will learn. You are asking the right question to the
wrong person. Look up references, study the pros and cons about
pointers. Code, code and then code again until you get it.
Sorry, I just don't understand.

Consider changing that, get a relevent book. The faq can help too.
 
E

Erik Wikström

In message <[email protected]
ps.com>


This is jargon I don't know. Does "reseat" mean to point a pointer at
some other address? That seems to the meaning of

Reseat means to make it refer to something else. A reference can only
refer to one thing, and you have to decide what that is when you create
it, with a pointer on the other hand you can change what it points to.
What exactly is going on here?

m is created and given the value 77, then the address of m is taken and
is stored in p. This means that p will now point to m.
j has been declared. No problems about that!
What has happened here?

When you create the reference ref you say that it refers to j. After
this you can think of ref as an alias for j, ref and j are now two
different names for the same thing.
Sorry, I just don't understand.

Since ref and j are the same thing, setting the value of ref is the same
as setting the value of j.

How about this analogy: there is this guy named Bob, when someone talks
about Bob they mean him. However Bob has a son, and when he talks about
Bob he call him "dad". In this case dad is a reference to Bob, they both
refer to the same thing.

Bob is the boss at a company, and when his employees talk about the boss
they talk about him, but if Bob should be sacked and a new boss hired
the employees would still talk about the boss, but no longer about Bob.
In this case "the boss" is a pointer, it refers to a person but
indirectly and who it refers to can thus be changed.
 
D

Diego Martins

If it's worth wasting people's time on Usenet, it's certainly worth
learning about yourself.

Michael, ignore the miserable ones and Usenet will be a wonderful place
 
S

Salt_Peter

Michael, ignore the miserable ones and Usenet will be a wonderful place

Thats the wrong attitude, 'the miserable ones' are those doing him a
favour. Different programmers employ different strategies and use
different means to achieve different goals and requirements. The
ultimate goal is to teach you how to teach yourself (thats what
schools do). Its required in C++.
 
D

Diego Martins

Thats the wrong attitude, 'the miserable ones' are those doing him a
favour. Different programmers employ different strategies and use
different means to achieve different goals and requirements. The
ultimate goal is to teach you how to teach yourself (thats what
schools do). Its required in C++.

you're right,
so teach how to teach instead of spreading misery
 
D

Diego Martins

I just did, they should ignore your remarks and pick up a book and
post here with relevent questions.
Sitting here and teaching a student pointers from scratch is misery.
Neither is it the goal nor intent of this newsgroup.
Perhaps you aren't aware of what this newsgroup is really about?http://www.parashift.com/c++-faq-lite/how-to-post.html

bullshit!
We know most of times the best approach is being kind.

You did nothing of value. For instance, if you had cited some
bibliography before, we'd be having a different conversation :)

Michael, you can try:
Thinking in C++ vol 1., Bruce Eckel, chapter 3 - section "Introduction
to pointers" <-- free on the web
The C++ Programming Language, Stroustroup, chapter 5
Accelerated C++, Koenig, chapter 10.1

have a horrible death, Salt_Peter
 
S

Salt_Peter

bullshit!
We know most of times the best approach is being kind.

You did nothing of value. For instance, if you had cited some
bibliography before, we'd be having a different conversation :)

Michael, you can try:
Thinking in C++ vol 1., Bruce Eckel, chapter 3 - section "Introduction
to pointers" <-- free on the web
The C++ Programming Language, Stroustroup, chapter 5
Accelerated C++, Koenig, chapter 10.1

have a horrible death, Salt_Peter

How cute:
a) you didn't even BOTHER to notice that i'm not the one who posted
what you acuse me of
b) What could possibly be so misereable about stating the above before
complaining?
c) i gave the OP a half decent introduction to pointers as is
d) being kind means don't be an ASSHOLE

plunck!
 
M

Matthias Buelow

Diego said:
We know most of times the best approach is being kind.

The kind of arrogance ("tell me that it is worth my time and effort")
the OP has displayed does not warrant a kind answer.
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top