说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C2-C_depth/index.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
2. A Deeper Dive into C Programming
上一章已经介绍了 C 语言编程的许多基础知识,现在我们更深入地探究 C 的细节。本章会重新讨论上一章中的许多主题,如数组、字符串和结构体,并更详细地展开。我们还会介绍 C 的指针变量和动态内存分配。指针(pointer)为访问程序状态提供了一层间接性,而动态内存分配(dynamic memory allocation)让程序在运行时能够适应大小和空间需求的变化:需要时分配更多空间,不再需要时释放空间。理解了如何以及何时使用指针变量和动态内存分配,C 程序员就能设计出既强大又高效的程序。
With many of the basics of C programming covered in the previous chapter, we now dive deeper into the details of C. In this chapter we revisit many of the topics from the previous chapter, such as arrays, strings, and structs, discussing them in more detail. We also introduce C’s pointer variables and dynamic memory allocation. Pointers provide a level of indirection to accessing program state, and dynamic memory allocation allows a program to adjust to changes in size and space needs as it runs, allocating more space as it needs it and freeing space it no longer needs. By understanding how and when to use pointer variables and dynamic memory allocation, a C programmer can design programs that are both powerful and efficient.
我们首先讨论程序内存的各个组成部分,这有助于理解后面介绍的许多主题。随着本章的推进,我们还会介绍 C 的文件 I/O,以及一些 C 的高级主题,包括库的链接和编译成汇编代码。
We begin with a discussion of the parts of program memory, as this will help in understanding many of the topics presented later. As the chapter progresses, we cover C file I/O and some advanced C topics including library linking and compiling to assembly code.