I am trying to put my learning effort into the most useful things.
What do pointers do that other things can't?
Michael Bell
-- 12 2568
Dnia Sun, 02 Dec 2007 14:07:10 GMT
Michael Bell <mi*****@beaver bell.co.uknapis a³(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.gliwi ce.pl
pgp: 0x90746E79 @ pgp.mit.edu
www: http://bla.thera.be
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
On Dec 2, 9:07 am, Michael Bell <mich...@beaver bell.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)
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.)
On Dec 2, 10:07 pm, Michael Bell <mich...@beaver bell.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.
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
--
In message <cd************ *************** *******@s8g2000 prg.googlegrou
ps.com>
Salt_Peter <pj*****@yahoo. comwrote:
On Dec 2, 9:07 am, Michael Bell <mich...@beaver bell.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
--
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.
On Dec 3, 7:23 am, Michael Bell <mich...@beaver bell.co.ukwrote :
In message <cddd48b2-aa1e-4ced-b180-703c22738...@s8 g2000prg.google grou
ps.com>
Salt_Peter <pj_h...@yahoo. comwrote:
On Dec 2, 9:07 am, Michael Bell <mich...@beaver bell.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 -
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
info@petermount.au.com
|
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 sorry to those who advice me to post it to java people
because I have already done it.
Jus want to know alternatives to pointers which can be used with
C.While pointers provide flexibility most bugs are with respect to
pointers.So will it not be...
|
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!
|
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 there something wrong in there?
--------------------------------------------------------------------
Types
A type is a definition for a sequence of storage bits. It gives the
meaning of the data stored in memory. If we say that the object a is an
|
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 better fits your needs than
shared_ptr, try a shared_ptr-based design first."
This tells me that intrusive pointers are faster and leaner than any of the
other alternative other than raw pointers.
http://www.boost.org/libs/smart_ptr/smarttests.htm
...
| |
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 the advantages of smart
pointers endlessly (which are currently used in all our C++ software; we
use the Boost smart pointers) as I'm seriously concerned that there is a
shift to raw pointers. We are not developing system software but rather...
|
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 little
pointers, obviously I avoid using them.
A few days ago when I was reading a book about programming, I was told
that pointers are the very essence of C language, if I couldn't use it
well, I'm a bad programmer, it's a big shock.
So now I'm...
|
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), _netguid(netguid) {}
|
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, internet, etc.) and
found out about the CLR and how all of the .NET languages access it, the
major difference being the syntax and structure of the individual
languages. What I'm wondering, since VB.NET is obviously easier to
learn/use than C#.NET and...
|
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...
|
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...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |