我不明白指针 [英] I don't understand pointers

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

问题描述

什么是指针?什么是解引用?如果 p 是一个指针,*p = some_valuep = other_value 有什么区别?p = &some_variable 是什么意思?什么是空指针?取消引用 NULL 指针时会发生什么?

What is a pointer? What is dereferencing? If p is a pointer, what is the difference between *p = some_value and p = other_value? What does p = &some_variable mean? What is a NULL pointer? What happens when you dereference a NULL pointer?

推荐答案

获取一叠黄色便签、铅笔、橡皮和钢笔.

Get a stack of yellow sticky notes, a pencil, an eraser, and a pen.

做个便利贴.用一条水平线把它分开.用钢笔在上面写下note #1".(此标记是永久性的).用铅笔在底部写下7".将其贴在计算机屏幕的左上角.

Take a sticky note. Divide it with a horizontal line. Write "note #1" on it with a pen in the top part. (This mark is permanent). Write "7" with a pencil in the bottom part. Stick it to the top left corner of your computer screen.

此注释表示存储整数值 7 的内存位置.顶部是地址.每个位置都有自己唯一的永久地址.底部是存储的.

This note represents a memory location that stores the integer value 7. The top part is the address. Each location has its own unique permanent address. The bottom part is the value being stored.

再做一个笔记,把它分成两个区域,然后在上面同样写上note #2"和13".把它贴在第一个旁边的屏幕上.这是另一个存储整数值的内存位置,这次是 13.

Take another note, divide it in two areas, and similarly write "note #2" and "13" on it. Stick it to your screen next to the first one. This is another memory location that stores an integer value, 13 this time.

现在记下另一个笔记.在注释的地址"部分写上注释#3".在笔记的值"部分写下note #1"(使用铅笔!).您刚刚创建了一个指针.把它贴在前两个音符旁边.

Now take another note. In the "address" part of the note write "note #3". In the "value" part of the note write "note #1" (use the pencil!). You have just created a pointer. Stick it next to the first two notes.

现在在一张单独的纸上写下几个速记:

Now write down a couple of shorthands on a separate piece of paper:

a    int     #1
b    int     #2
p    int*    #3

这代表一个符号表.abp 是*变量名`.在下一列中我们有它们的类型,第三列包含它们的地址.

This represents a symbol table. a, b and p are *variable names`. In the next column we have their types, and the third column contains their addresses.

现在让我们做一些 C 语句.

Now let's do some C statements.

a = 77;

在符号表中查找 a.它是#1.擦除此笔记的值部分并改写 77(这就是橡皮擦派上用场的地方).

Look up a in your symbol table. It is #1. Erase the value part of this note and write 77 instead (that's where the eraser comes handy).

b = 2017;

这与上一个完全类似.

a = b;

在符号表中查找ab.他们分别是#1和#2.看看注释 #2 在它的价值"部分有什么.将该数字复制到注释 #1 的值部分.

Look up a and b in your symbol table. They are #1 and #2 respectively. See what note #2 has in its "value" part. Copy that number to the value part of note #1.

*p = 42;

在符号表中查找 p.这是注释#3.现在找到注释 #3 的值部分中写有数字的注释.这就是称为取消引用的操作.在上面的符号中用 * 表示.擦除 那个 注释的值部分中的任何内容(不是 #3,#3 是 指向)并改写 42.

Look up p in your symbol table. That's note #3. Now find the note with the number written in the value part of note #3. That's the operation called dereferencing. It is denoted by the * in the notation above. Erase whatever is in the value part of that note (not the #3, the one #3 is pointing at) and write 42 instead.

*p = b; 

自己做.这是你应该得到的:

Do this yourself. Here's what you should get:

p = &b;

再次在符号表中查找pb(分别为#3 和#2).现在取 #2 的 地址部分(这是由 & 表示的操作,不出所料,通常称为 address-of 运算符)和将其复制到#3 的值部分(当然要删除之前的内容).

Again, look up p and b in your symbol table (#3 and #2 respectively). Now take the address part of #2 (that's the operation represented by &, often called, unsurprisingly, the address-of operator) and copy it to the value part of #3 (of course erase what was there beforehand).

*p = 42;

现在应该很容易了.

现在在#4 下创建另一个指针.将其作为 q 归档到您的符号表中.

Now create another pointer under #4. File it in your symbol table as q.

q = NULL;

找到q(即#4 注释)并在值部分写入note #0.请注意,没有注释 #0.那是一个空指针.它不指向任何地址.

Find q (that's the #4 note) and write note #0 in the value part. Note that there is no note #0. That's a null pointer. It doesn't point to any address.

注意:标准没有强制要求实际表示的空指针为 0.它可能是 note #99999999note #-1或者其他什么,只要它与所有现有的笔记不同

Caution: the standard doesn't mandate that the null pointer is actually represented is 0. It could have been note #99999999 or note #-1 or whatever, as long as it's distinct from all existing notes

p = q;

自己做.这是你应该得到的:

Do this yourself. Here's what you should get:

*p = 42;

应该怎么办?从表面上看,您应该使用 p(即 #3),找出它指向哪个音符,然后更改该音符.但是它现在指向注释#0,并且没有这样的注释.您调用了未定义的行为.C 标准明确拒绝指定您的程序应该做什么.在实践中,它很可能会因诸如分段错误(核心转储)"或程序 <程序名称> 已停止工作"之类的消息而崩溃.

What should happen? Ostensibly you should take p (that's #3), find out which note it points to, and change that note. But it points to note #0 now, and there's no such note. You have invoked undefined behaviour. The C standard explicitly declines to specify what your program should do. In practice it is likely to crash with a message such as "segmentation fault (core dumped)" or "program <programname> has stopped working".

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

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