Pointers on C——6 Pointers.1

Itʹs time we talk in detail about pointers, because we will be using them constantly throughout the rest of the book. You may already be familiar with some or all of the background information discussed in this chapter. If you arenʹt, study it carefully because your understanding of pointers will rest on this foundation.

是详细讨论指针的时候了,因为在本书的剩余部分,我们将会频繁地使用指针。你可能已经熟悉了本章所讨论的部分或全部背景信息。但是,如果你对此尚不熟悉,请认真学习,因为你对指针的理解将建立在这个基础之上。

6.1 Memory and Addresses

As mentioned earlier, we can view the computerʹs memory as a row of houses on a long street. Each house is capable of holding data and is identified by a house number.

我在前面提到过,我们可以把计算机的内存看作是一条长街上的一排房屋。每座房子都可以容纳数据,并通过一个房号来标识。

This analogy is useful but limited. Computer memories are composed of millions of bits, each capable of holding either the value 0 or the value 1. This limited range of values is not terribly useful, so bits are usually grouped together and treated as a unit in order to store a wider range of values. Here is a picture of a few memory locations on a real machine.

这个比喻颇为有用,但也存在局限性。计算机的内存由数以亿万计的位(bit)组成,每个位可以容纳值0或l。由于一个位所能表示的值的范围太有限,所以单独的位用处不大,通常许多位合成一组作为一个单位,这样就可以存储范围较大的值。这里有一幅图,展示了现实机器中的一些内存位置。


Each of these locations is called a byte, and each contains as many bits as necessary to store a single character. On many modern machines, each byte contains eight bits,which can store unsigned integers from 0 to 255 or signed integers from ‐128 to 127.The previous diagram does not show the contents of the locations, though every location in memory always contains some value. Each byte is identified by an address,which is represented in the diagram by the numbers above each box.

这些位置的每一个都被称为字节( byte),每个字节都包含了存储一个字符所需要的位数。在许多现代的机器上,每个字节包含8个位,可以存储无符号值O至255,或有符号值-128至127。上面这张图并没有显示这些位置的内容,但内存中的每个位置总是包含一些值。每个字节通过地址来标识,如上图方框上面的数字所示。

To store even larger values, we take two or more bytes and treat them as if they were a single, larger unit of memory. For example, many machines store integers in words, each composed of two or four bytes. Here are the same memory locations, this time viewed as four‐byte words.

为了存储更大的值,我们把两个或更多个字节合在一起作为一个更大的内存单位·例如,许多机器以字为单位存储整数,每个字一般由2个或4个字节组成。下面这张图所表示的内存位置与上面这张图相同,但这次它以4个字节的字来表示。


Because they contain more bits, each word is capable of holding unsigned integers from 0 to 4,294,967,295 (232 – 1) or signed integers from –2,147,483,648 (–231) to 2,147,483,647 (231 – 1).

由于它们包含了更多的位,每个字可以容纳的无符号整数的范围是从0至4294967295(232 – 1),可以容纳的有符号整数的范围是从-2147483648(–231)至2147483647(231 – 1)。

Note that even though a word contains four bytes, it has only one address. It is machine‐dependent whether the address of a word is the address of the leftmost byte in the word or the rightmost. Boundary alignment is another hardware issue. On machines with this requirement, integers can only begin with certain bytes, usually those whose addresses are multiples of two or four. But these issues are the hardware designerʹs problem, and they rarely affect C programmers. We are interested in only two issues:

注意,尽管一个字包含了4个字节,它仍然只有一个地址。至于它的地址是它最左边那个字节的位置还是最右边那个字节的位置,不同的机器有不同的规定。另一个需要注意的硬件事项是边界对齐(boundary alignment)。在要求边界对齐的机器上,整型值存储的起始位置只能是某些特定的字节,通常是2或4的倍数。但这些问题是硬件设计者的事情,它们很少影响C程序员。我们只对两件事情感兴趣

1. Each location in memory is identified by a unique address.

内存中的每个位置由一个独一无二的地址标识。

2. Each location in memory contains a value.

内存中的每个位置都包含一个值。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值