473,405 Members | 2,167 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,405 software developers and data experts.

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

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

Michael Bell

--
Dec 2 '07 #1
12 2531
Dnia Sun, 02 Dec 2007 14:07:10 GMT
Michael Bell <mi*****@beaverbell.co.uknapisa³(a):
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,
--
Tomasz bla Fortuna
jid: bla(at)af.gliwice.pl
pgp: 0x90746E79 @ pgp.mit.edu
www: http://bla.thera.be
Dec 2 '07 #2
On 2007-12-02 15:07, Michael Bell wrote:
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.

--
Erik Wikström
Dec 2 '07 #3
On Dec 2, 9:07 am, Michael Bell <mich...@beaverbell.co.ukwrote:
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)
Dec 3 '07 #4
Michael Bell wrote:
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.)
Dec 3 '07 #5
On Dec 2, 10:07 pm, Michael Bell <mich...@beaverbell.co.ukwrote:
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.
Dec 3 '07 #6
In message <47**********************@news.tdc.fi>
Juha Nieminen <no****@thanks.invalidwrote:
Michael Bell wrote:
>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.)
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
--
Dec 3 '07 #7
In message <cd**********************************@s8g2000prg.g ooglegrou
ps.com>
Salt_Peter <pj*****@yahoo.comwrote:
On Dec 2, 9:07 am, Michael Bell <mich...@beaverbell.co.ukwrote:
>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)
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?
int j;
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

--
Dec 3 '07 #8
Michael Bell wrote:
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.
Dec 3 '07 #9
On Dec 3, 7:23 am, Michael Bell <mich...@beaverbell.co.ukwrote:
In message <cddd48b2-aa1e-4ced-b180-703c22738...@s8g2000prg.googlegrou
ps.com>
Salt_Peter <pj_h...@yahoo.comwrote:


On Dec 2, 9:07 am, Michael Bell <mich...@beaverbell.co.ukwrote:
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)

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
>
int m(77);
p = &m; // reseated pointer

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.
>
int j;

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.
>
// ref = m; // error
ref = 55; // ok, j is now set to 55

Sorry, I just don't understand.
Consider changing that, get a relevent book. The faq can help too.
>
Michael Bell

--- Hide quoted text -

- Show quoted text -
Dec 4 '07 #10
On 2007-12-03 13:23, Michael Bell wrote:
In message <cd**********************************@s8g2000prg.g ooglegrou
ps.com>
Salt_Peter <pj*****@yahoo.comwrote:
>___
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
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.
>int m(77);
p = &m; // reseated pointer


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.
>int j;
j has been declared. No problems about that!
>int& ref = j; // ref is permanently bound
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.
>// ref = m; // error
ref = 55; // ok, j is now set to 55

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.

--
Erik Wikström
Dec 4 '07 #11
On Dec 3, 11:15 am, Matthias Buelow <m...@incubus.dewrote:
Michael Bell wrote:
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.
Michael, ignore the miserable ones and Usenet will be a wonderful place
Dec 4 '07 #12
Diego Martins wrote:
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.
Dec 6 '07 #13

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

Similar topics

11
by: Peter Mount | last post by:
Hello Are there any good online tutorials that explain pointers in ANSI C? I've covered pointers in my course but I'm just having trouble "getting it to sink in". Thanks Peter Mount...
19
by: s.subbarayan | last post by:
Dear all, I had this following doubt,while java is able to carryon with out pointers why C language cant be modified to remove pointer?Hows java able to do this with out pointers? I jus plead...
20
by: Bill Potter | last post by:
I am a learning programmer in C and i want to know why some one would use pointers instead of going direct!
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
2
by: Steven T. Hatton | last post by:
It's my understanding that intrusive pointers are frowned upon. For example this is from the boost::intrusive_ptr documentation: "As a general rule, if it isn't obvious whether intrusive_ptr...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
69
by: Yee.Chuang | last post by:
When I began to learn C, My teacher told me that pointer is the most difficult part of C, it makes me afraid of it. After finishing C program class, I found that all the code I wrote in C contains...
16
by: John Doe | last post by:
Hi, I wrote a small class to enumerate available networks on a smartphone : class CNetwork { public: CNetwork() {}; CNetwork(CString& netName, GUID netguid): _netname(netName),...
49
by: Zach | last post by:
After having taken a looong break from VB (last used 6.0), I started getting back into programming again, this time with VS 2005. I began reading up on VB.NET from various sources (books,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...
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,...

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.