什么可以指针做其他事情不能做? [英] What can pointers do that other things can't do?
问题描述
我正在努力将我的学习努力投入最有用的事情。
指针做什么其他事情不可以?
迈克尔贝尔
-
Dnia Sun,2007年12月2日14:07:10 GMT
Michael Bell< mi ***** @ beaverbell.co.uknapisa3(a):
我正在努力学习进入最有用的东西。
指针做什么其他事情不可能?
好吧,他们可以......点?我想你应该得到一些与C相关的书。
如果你没有指针,你甚至无法操作文本
(字符串)。
你读过c-faq吗?
最好的问候,
-
Tomasz bla Fortuna
jid:bla(at)af.gliwice.pl
pgp:0x90746E79 @ pgp.mit.edu
www: http://bla.thera.be
< blockquote>在2007-12-02 15:07,Michael Bell写道:
我正在努力将我的学习努力投入最有用的事情。
指针做什么其他事情不可能?
我认为其他的东西你的意思是引用(其他任何东西都会比较像苹果和橘子一样,比较指针与
引用更像苹果和梨:至少有一些
的相似之处)。有许多不同之处:
指针可以是空指针(指向什么都没有),而引用
总是(应该)裁掉某些东西。您可以随时测试指针
是否为空指针,但您无法判断引用是否引用了有效的
对象。
指针是对象在他们自己的权利,你可以创建一个指针
指针(获取指针的地址),而你永远不会
创建指向引用或引用的指针参考。
您可以更改指针指向的内容,您永远不能更改
引用所指的内容。
>
你可以对一个指针执行算术运算,即如果p是一个指向数组第一个元素的
指针那么(p + 5)指的是第6个
元素相同的数组。
从程序的角度来看,对象的
引用与对象本身没有区别,你可以在
中对待
引用(执行相同的操作,用作参数等)。另一方面,指针只是一个对象
指向一些其他对象和对指针执行的操作
不会影响它指向的对象。
-
Erik Wikstr ?? m
12月2日上午9:07,Michael Bell < mich ... @ beaverbell.co.ukwrote:
我正努力将我的学习努力投入最有用的事情。
指针做什么其他事情不可以?
Michael Bell
-
指针是nothing_at_all的地址,直到它实际指向
有效的东西。
int * p;
p是一个带有剩余值的指针(垃圾地址)。没有人关心
现在可能拥有什么值。
由于语句没有调用(原始)构造函数,因此它的
无效地址为什么都没有。
上面的陈述中的危险是假装指针是有效的(bangling指针)
。
int n(99);
int * p =& n;
啊,现在调用了一个ctor。现在使用
变量的地址初始化指针。在这个
点之间没有区别n或* p。你问的差异是什么?指针p可以修改为
指向另一个对象。
int m(77);
p =& m ; //重新安装指针
这将我们带到引用主题。你不能重新安装
参考。它永久地与目标绑定。
语法完全不同。
int j;
int& ; ref = j; // ref永久绑定
// ref = m; //错误
ref = 55; //好吧,j现在设置为55
___
所有这一切都说明:
a)使用引用代替你可以随时使用指针。它是我知道的最好的
杀虫剂。
b)如果你必须使用指针,总是初始化它(如果你不能给它
它是一个有效的地址 - 然后将其归零)
I am trying to put my learning effort into the most useful things.
What do pointers do that other things can''t?
Michael Bell
--
Dnia Sun, 02 Dec 2007 14:07:10 GMT
Michael Bell <mi*****@beaverbell.co.uknapisa3(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
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...@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)
这篇关于什么可以指针做其他事情不能做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!