指针指南 [英] Pointer Guide

查看:72
本文介绍了指针指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看本指南的清晰度,准确性等等。所以我可以

希望编译一个关于如何在C中使用指针的非常好的教程,

包括高级主题,这很容易理解并公开

的详细信息。

http://thelinuxlink.net/~fingolfin/pointer-guide/


我的电子邮件地址是假的。要真正与我联系,请发送邮件至:
fi***********************@thelinuxlink.net ,减去公然的

反垃圾邮件组件。


-

我很开心

''因为今天我找到了我的朋友

他们在我脑海里


- Kurt Cobain

Please review this guide for clarity, accuracy, etc. so I can
hopefully compile a very good tutorial on how to use pointers in C,
including advanced topics, that is easy to follow and exposes the
details.

http://thelinuxlink.net/~fingolfin/pointer-guide/

My e-mail address is fake. To really contact me, send mail to:
fi***********************@thelinuxlink.net, subtracting the blatant
anti-spam component.

--
I''m so happy
''Cause today I found my friends
They''re in my head

-- Kurt Cobain

推荐答案



2004年6月18日星期五,Foobarius Frobinium写道:

On Fri, 18 Jun 2004, Foobarius Frobinium wrote:

请查看本指南的清晰度,准确性等,以便我能够
希望编译一个非常好的教程关于如何在C中使用指针,包括高级主题,易于理解和暴露
详细信息。

http://thelinuxlink.net/~fingolfin/pointer-guide/



您对左值的使用和rvalue稍微不标准,但是IMO足够一致。通常的术语是/ b $ b表示/表达式/ /是左值或左值,取决于

表达式是否表示带有/ address /的对象。

您使用左值表示/地址/,和rvalue意思是

/ value /。足够一致,但不喜欢学生。


第一个示例程序的HTML化,即

开始的''/ * vim:ts = 4 * /'',在白色

背景下是绝对不可读的(大多数GUI Web浏览器中的默认设置)。不要
在白色背景上使用浅绿色或青色文字。首选

没有着色,特别是因为你似乎无法决定

a一致的方案:为什么''fprintf''黑色但''stdout'' magenta?


然而,HTML可以在教程的正文中使用./ $
。考虑在他们的首次出场时加上新的条款,以便更容易地浏览大的

段落;我建议使用< tt>< / tt>或者< i>< / i>

围绕文本中的代码片段,例如''foo''和''frob''。


行''/ * vim:ts = 4 * /''完全没必要你的教程BTW,并且只会混淆非Unix类型。

建议从HTML源代码中移除硬标签

,因为我怀疑许多浏览器可以有效地与它们交易
。建议使用< pre>格式化源代码。


建议''返回0;''来自''main''。这不是C99要求的
,但在C90和C99都是一个很好的做法。并且

你应该写的是在

新手中灌输良好做法...


因此,返回类型为* bar是int ...删除

单词return从这句话; operator / yield /

值。功能/返回/。一元*不是一个函数,

因此/返回/没有。


它们的相关性将被揭示......插入

" not"在是之前当然。


s / interchangably / interchangeably /


"而且因为char'的长度是一个字节(至少在

我知道的所有系统)...... ''sizeof(char)''是1(字节)by

定义。是否有任何特定的系统。有一个

机器级概念字节与C

编程无关。但是,之后两者都没有使用实际数字

来引用RAM中的地址。所以你只需要给b
选择能满足学生的措辞。建议

只需删除括号注释。


" ...简而言之,它是一种数据类型,用于
通过其左值引用其他数据。令人困惑。

首先,''short''是一种数据类型,所以你只是主要将
混淆了撇油器,他们并没有太多关注

你的语法兴旺。其次,你的

语法或你的内容在这句话的后半部分是错误的。指针是一个通过其rvalue引用其他数据的对象。 (它/ value /),或者

等价于指针/包含/另一个数据''的左值

(它的/地址/)。


数组访问的数据无法更改。

因此它们不是指针。语法再次出现!

首先,数组*访问的数据可以*改变;

考虑''arr [0] = 42; arr [0] = 43;''。其次,数组*访问的
数据可以是指针;考虑

''int * arp [10];''。你的意思是,一个数组不能被改变

到''指向'其他数据;因此数组不是指针。


s / syntax sugar / syntactic sugar /


你的HTML化主要搞砸了Basic

指针算术,它实际上吃的地方循环中的''for''

关键字。降低

着色的另一个原因。

"当您为数字类型添加数字时,例如int

或双倍,你可以直接添加。但是当你在
指针上执行此操作时,该数字会自动乘以指针指向的类型的大小

(以字节为单位)。这个

是通常解释的,是真的。但是你的导入是有点愚蠢的,因为你从来没有说过你的意思直接

加法。显然,将一个指针添加到

指针的机器代码与将'1'添加到''int''的机器代码不同;

但是用于将1添加到a的机器代码''double''往往是* b $ b *远*更复杂。然而你把后两个称为直接

另外! IMO更简单地告诉

读者,指针的添加是/定义/它是b / b的方式,让他们自己确定是否'' s

很奇怪。


s / deference / dereference /


" ...算术运算符优先于指针

[取消引用]无论如何......这只是扁平/错误/。

删除它,以及周围的误导。


最后,虽然教程显然尚未

已完成,我必须敦促您在[]

间接运营商上添加资料。读者目前正在思考

''*(kochba + i)''是取消引用指针的唯一方法。

[更轻松一点,我必须说我从未见过''kochba''使用

作为''foo,bar,......'的第三个元素!非常古怪。]

HTH,

-Arthur


Your use of "lvalue" and "rvalue" is slightly non-standard, but
IMO consistent enough. The usual terminology would have it
that an /expression/ /is/ an lvalue or an rvalue, depending on
whether the expression denotes an object with an /address/.
You use "lvalue" to mean /address/, and "rvalue" to mean
/value/. Consistent enough, but irking to pedants.

The HTMLification of the first sample program, the one that
begins ''/* vim:ts=4 */'', is absolutely unreadable on a white
background (the default in most GUI web browsers). Do not
use light green or cyan text on a white background. Prefer
no coloration, especially since you can''t seem to decide on
a consistent scheme: why is ''fprintf'' black but ''stdout''
magenta, for example?

HTML could be used to great advantage in the body text of
the tutorial, however. Consider bolding new terms on their
first appearance, to make it easier to skim the large
paragraphs; and I recommend using <tt></tt> or <i></i>
around code snippets in the text such as ''foo'' and ''frob''.

The line ''/* vim:ts=4 */'' is completely unnecessary to
your tutorial, BTW, and will only confuse non-Unix types.
Suggest removing the hard tabs from your HTML source code
as well, since I doubt many browsers can effectively deal
with them. Suggest the use of <pre> to format source code.

Suggest ''return 0;'' from ''main''. This is not required
by C99, but is a good practice in both C90 and C99. And
you are supposedly writing to instill good practices in
newbies...

"Therefore, the return type of *bar is int..." Delete
the word "return" from this sentence; operators /yield/
values. Functions /return/. Unary * is not a function,
and thus /returns/ nothing.

"The relevance of them will be revealed yet..." Insert
"not" before "be," of course.

s/interchangably/interchangeably/

"And since char''s are one byte in length (at least on
all systems I know)..." ''sizeof(char)'' is 1 (byte) by
definition. Whether any particular "system" has a
machine-level concept of "byte" is not relevant to C
programming. But then neither is using actual numbers
to refer to "addresses in RAM," so you''ll just have to
pick a wording that will satisfy the pedants. Suggest
simply removing the parenthetical comment.

"...in short it is a data type that is made to
reference other data through its lvalue." Confusing.
Firstly, ''short'' is a data type, so you''ve just majorly
confused the skimmers, who aren''t paying much attention
to your grammatical flourishes. Secondly, either your
grammar or your content is wrong in the second half of
that sentence; a pointer is an object which references
other data through its "rvalue" (its /value/), or
equivalently a pointer /contains/ another datum''s "lvalue"
(its /address/).

"The data an array accesses cannot be changed.
Therefore they are not pointers." Grammar strikes again!
First, the data accessed by an array *can* be changed;
consider ''arr[0] = 42; arr[0] = 43;''. Secondly, the
data accessed by an array *can* be pointers; consider
''int *arp[10];''. You mean, "An array cannot be changed
to ''point'' to other data; therefore arrays are not pointers."

s/syntax sugar/syntactic sugar/

Your HTMLification is screwed up majorly in "Basic
Pointer Arithmetic," where it actually "eats" the ''for''
keyword in your loop. Yet another reason to drop the
colorization.

"When you add a number to a number type, such as an int
or double, you get straight addition. But when you do it on
pointers, the number is automatically multiplied by the size
(in bytes) of the type to which the pointer points." This
is how it''s usually explained, true. But your lead-in is
somewhat silly, since you never say what you mean by "straight
addition." Obviously the machine code for adding 1 to a
pointer is different from that for adding 1 to an ''int'';
but the machine code for adding 1 to a ''double'' is often
*far* more complicated. Yet you call the latter two "straight"
addition! IMO it would make more sense simply to tell the
reader that addition on pointers is /defined/ the way it
is, and let them determine for themselves whether it''s
weird or not.

s/deference/dereference/

"...the arithmetic operators take precedence over pointer
[dereference] anyway..." This is just flat-out /wrong/.
Remove it, and the surrounding misdirection.

Finally, although the tutorial is obviously not yet
completed, I must urge you to add material on the []
indirection operator. The reader is currently left thinking
that ''*(kochba+i)'' is the only way to dereference a pointer.
[On a lighter note, I must say I''ve never seen ''kochba'' used
as the third element of ''foo, bar,...''! Very... quirky.]
HTH,
-Arthur


" Arthur J 。O'Dwyer < aj*@nospam.andrew.cmu.edu>在消息新闻中写道:< Pi ********************************** @ unix47。 andrew.cmu.edu> ...
"Arthur J. O''Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi**********************************@unix47. andrew.cmu.edu>...
2004年6月18日星期五,Foobarius Frobinium写道:
On Fri, 18 Jun 2004, Foobarius Frobinium wrote:

请查看本指南的清晰度,准确性等所以我希望能够编写一个关于如何在C中使用指针的非常好的教程,包括高级主题,这些主题很容易理解,并且会公开
详情。

http://thelinuxlink.net/~fingolfin/pointer-guide/



< snip rest of useful,verbose critique>

" ...算术运算符优先于指针
[取消引用]无论如何......这只是扁平/错误/。
删除它,以及周围的错误方向。


他们不是吗?我发誓他们做了......该死的,我得检查一下。也许

gcc有问题,但也许我只是傻了

最后,虽然教程显然尚未完成,但我必须敦促你在[]
间接运算符上添加材料。读者目前正在思考
''*(kochba + i)''是取消引用指针的唯一方法。
[更轻松一点,我必须说我从未见过' 'kochba''使用
作为''foo,bar,......'的第三个元素!非常......很古怪。]


是的,接下来我计划的是[]形式如何工作,并且

将其重新绑定到数组和指针的等价性。至于

''kochba'',你可能知道的任何犹太黑客都可以告诉你

的Simeon Bar Kochba,他发明了地下操作作为元素

游击战(即Whack-A-Guerilla),在Dien之前1800年

Bien Phu。这是真的!


在希望有用的数据中出现的奇怪之处是我青少年无聊的一部分,并且可能使阅读更容易。

HTH,
-Arthur


<snip rest of useful, verbose critique>
"...the arithmetic operators take precedence over pointer
[dereference] anyway..." This is just flat-out /wrong/.
Remove it, and the surrounding misdirection.
They don''t?? I woulda sworn they did...damn, I''ll have to check. Maybe
something is wrong with gcc, but maybe I was just being stupid

Finally, although the tutorial is obviously not yet
completed, I must urge you to add material on the []
indirection operator. The reader is currently left thinking
that ''*(kochba+i)'' is the only way to dereference a pointer.
[On a lighter note, I must say I''ve never seen ''kochba'' used
as the third element of ''foo, bar,...''! Very... quirky.]
Yes, the very next thing I was planning was how the [] form works, and
tie that back into the equivalency of arrays and pointers. As for
''kochba'', any Jewish hackers you might know might be able to tell you
of Simeon Bar Kochba, who invented subsurface operations as an element
of guerilla warfare (i.e., Whack-A-Guerilla), 1800 years before Dien
Bien Phu. It''s true!

Quirkiness in amidst hopefully useful data is all part of my
adolescent inanities, and probably makes the reading a little easier.

HTH,
-Arthur



2004年6月18日12:07:20 -0700, fo ****** @ youremailbox.com (Foobarius

Frobinium)在comp.lang.c中写道:
On 18 Jun 2004 12:07:20 -0700, fo******@youremailbox.com (Foobarius
Frobinium) wrote in comp.lang.c:
请查看本指南的清晰度,准确性等等。所以我希望能够编写一个关于如何在C中使用指针的非常好的教程,
包括高级主题,易于理解并公开
详细信息。

http://thelinuxlink.net/~fingolfin/pointer-guide/

我的电子邮件地址是假的。要真正与我联系,请发送邮件至:
fi***********************@thelinuxlink.net ,减去公然的反垃圾邮件组件。
Please review this guide for clarity, accuracy, etc. so I can
hopefully compile a very good tutorial on how to use pointers in C,
including advanced topics, that is easy to follow and exposes the
details.

http://thelinuxlink.net/~fingolfin/pointer-guide/

My e-mail address is fake. To really contact me, send mail to:
fi***********************@thelinuxlink.net, subtracting the blatant
anti-spam component.




我不同意Arthur。你在第一段中有这个:


"左值是内存中变量'的地址,而右值是

的实际内容变量。


你完全误解了C中左值和左值的概念。


在片段中:


int x;

x = 3;


....整数常量字面值''3'是一个右值

与任何变量都没有任何关系。


C标准在一个句子的一部分中定义术语''左值':


"左值是一个具有对象类型或不完整类型的表达式

除了void" [6.3.2.1 P. 1]


请注意,地址或变量这两个词都没有出现在

这个定义中。


另请注意,C标准已从该语言的

官方条款中删除了'rvalue'一词。它仅在脚注中提到:


有时称为rvalue?是在这个国际标准中描述为表达式的??值的b $ b。 [脚注53]


稍后,在你的第二句中,你说:


"指针是一个变量, rvalue是另一个

变量的左值。


这是完全错误的。指针对象的值是

''地址''。指针对象本身就是左值。

包含的地址不是任何一种左值。对

指针执行取消引用(指向void的指针除外)会创建一个带有

对象类型的表达式,因此* p是一个左值表达式。


说指针的值是左值就像是说

int的值是左值。


考虑:


int x = 3;


....'''x''是一个左值,值为3。


现在让我们添加:


int * ip =& x;


....''* ip''是一个左值,它是对象类型(类型为

int)的表达式,其值为''x'',即3.


....''ip''是一个左值,一个对象类型的表达式(类型指向

int),其值为''x' '。


但''ip''的内容,即x的地址,不是左值,

不再是''''的内容x'',即整数值3,是。

您是否真的获得并阅读了ISO C标准的副本?

PDF版本可用于



I disagree with Arthur. You have this in the first paragraph:

"The lvalue is the variable''s address in memory, and the rvalue is the
actual content of the variable."

You completely misunderstand the concepts of lvalues and rvalues in C.

In the snippet:

int x;
x = 3;

....the integer constant literal ''3'' is an rvalue that has nothing at
all to do with any variable.

The C standard defines the term ''lvalue'' in part of a single sentence:

"An lvalue is an expression with an object type or an incomplete type
other than void" [6.3.2.1 P. 1]

Notice that the neither of the words ''address'' or ''variable'' appear in
this definition.

Also note that the C standard has dropped the word ''rvalue'' from the
official terms of the language. It is mentioned only in a footnote:

"What is sometimes called ??rvalue?? is in this International Standard
described as the ??value of an expression??" [Footnote 53]

Just a little bit later, in your second sentence, you say:

"A pointer is a variable whose rvalue is the lvalue of another
variable."

This is just plain wrong. The value of a pointer object is an
''address''. A pointer object itself is an lvalue. The address that it
contains is not any sort of lvalue. Performing a dereference on a
pointer (other than pointer to void) creates an expression with an
object type, so *p is an lvalue expression.

Saying that the value of a pointer is an lvalue is like saying that
the value of an int is an lvalue.

Consider:

int x = 3;

....''x'' is an lvalue, and has the value 3.

Now let''s add:

int *ip = &x;

....''*ip'' is an lvalue, which is an expression of object type (type
int) with the value of ''x'', namely 3.

....''ip'' is an lvalue, an expression of object type (type pointer to
int) with the value address of ''x''.

But the contents of ''ip'', namely the address of x, is not an lvalue,
anymore than the contents of ''x'', namely the integer value 3, is.

Have you actually obtained and read a copy of the ISO C Standard? The
PDF version is available for


这篇关于指针指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆