3118
备注:
|
2206
|
删除的内容标记成这样。 | 加入的内容标记成这样。 |
行号 1: | 行号 1: |
[[Navigation(slideshow)]] | ## page was renamed from Pointers and Arrays [[Navigation(slides)]] |
行号 3: | 行号 4: |
= Chapter 5 - Pointers and Arrays = | = Chapter 5 - Pointers and Arrays 指针与数组 = |
行号 5: | 行号 6: |
__A pointer is a variable that contains the address of a variable.__ Pointers are much used in C, partly because they are sometimes the only way to express a computation, and partly because they usually lead to more compact and efficient code than can be obtained in other ways. Pointers and arrays are closely related; this chapter also explores this relationship and shows how to exploit it. | A pointer is a variable that contains the address of a variable. Pointers are much used in C, partly because they are sometimes the only way to express a computation, and partly because they usually lead to more compact and efficient code than can be obtained in other ways. Pointers and arrays are closely related; this chapter also explores this relationship and shows how to exploit it. 指针是一种保存变量地址的变量。在C语言中,指针的使用非常广泛,原因之一是,指针常常是表达某个计算的唯一途径,另一个原因是,同其他方法比较起来,使用指针通常可以生成更高效、更紧凑的代码。指针与数组之间的关系十分密切,我们将在本章中讨论它们之间的关系,并探讨如何利用这种关系。 |
行号 9: | 行号 12: |
指针和goto语句一样,会导致程序难以理解。如果使用者粗心,指针很容易就指向了错误的地方。但是,如果谨慎的使用指针,便可以利用它写出简单、清晰的程序。在本章中我们将尽力说明这一点。 |
|
行号 11: | 行号 16: |
1. ["/5.1 Pointers and Addresses"] 1. ["/5.2 Pointers and Function Arguments"] 1. ["/5.3 Pointers and Arrays"] 1. ["/5.4 Address Arithmetic"] 1. ["/5.5 Character Pointers and Functions"] 1. ["/5.6 Pointer Arrays; Pointers to Pointers"] 1. ["/5.7 Multi-dimensional Arrays"] 1. ["/5.8 Initialization of Pointer Arrays"] 1. ["/5.9 Pointers vs. Multi-dimensional Arrays"] [[Navigation(children)]] |
ANSI C的一个最重要的变化是,它明确地制定了操纵指针的规则。事实上,这些规则已经被很多优秀的程序设计人员和编译器所采纳。此外,ANSI C适用类型void *(指向void的指针)代替char *作为通用指针的类型。 |
行号 22: | 行号 18: |
== 5.10 Command-line Arguments == In environments that support C, there is a way to pass command-line arguments or parameters to a program when it begins executing. When main is called, it is called with two arguments. The first (conventionally called argc, for argument count) is the number of command-line arguments the program was invoked with; the second (argv, for argument vector) is a pointer to an array of character strings that contain the arguments, one per string. We customarily use multiple levels of pointers to manipulate these character strings. The simplest illustration is the program echo, which echoes its command-line arguments on a single line, separated by blanks. That is, the command {{{#!cplusplus echo hello, world }}} prints the output {{{#!cplusplus hello, world }}} By convention, argv[0] is the name by which the program was invoked, so argc is at least 1. If argc is 1, there are no command-line arguments after the program name. In the example above, argc is 3, and argv[0], argv[1], and argv[2] are "echo", "hello,", and "world" respectively. The first optional argument is argv[1] and the last is argv[argc-1]; additionally, the standard requires that argv[argc] be a null pointer. attachment:pic511.gif The first version of echo treats argv as an array of character pointers: {{{#!cplusplus #include <stdio.h> /* echo command-line arguments; 1st version */ main(int argc, char *argv[]) { int i; for (i = 1; i < argc; i++) printf("%s%s", argv[ |
[[Include(^TCPL/5\.[0-9][0-9] .*, , titlesonly)]] |
Chapter 5 - Pointers and Arrays 指针与数组
A pointer is a variable that contains the address of a variable. Pointers are much used in C, partly because they are sometimes the only way to express a computation, and partly because they usually lead to more compact and efficient code than can be obtained in other ways. Pointers and arrays are closely related; this chapter also explores this relationship and shows how to exploit it.
指针是一种保存变量地址的变量。在C语言中,指针的使用非常广泛,原因之一是,指针常常是表达某个计算的唯一途径,另一个原因是,同其他方法比较起来,使用指针通常可以生成更高效、更紧凑的代码。指针与数组之间的关系十分密切,我们将在本章中讨论它们之间的关系,并探讨如何利用这种关系。
Pointers have been lumped with the goto statement as a marvelous way to create impossible-to-understand programs. This is certainly true when they are used carelessly, and it is easy to create pointers that point somewhere unexpected. With discipline, however, pointers can also be used to achieve clarity and simplicity. This is the aspect that we will try to illustrate.
指针和goto语句一样,会导致程序难以理解。如果使用者粗心,指针很容易就指向了错误的地方。但是,如果谨慎的使用指针,便可以利用它写出简单、清晰的程序。在本章中我们将尽力说明这一点。
The main change in ANSI C is to make explicit the rules about how pointers can be manipulated, in effect mandating what good programmers already practice and good compilers already enforce. In addition, the type void * (pointer to void) replaces char * as the proper type for a generic pointer.
ANSI C的一个最重要的变化是,它明确地制定了操纵指针的规则。事实上,这些规则已经被很多优秀的程序设计人员和编译器所采纳。此外,ANSI C适用类型void *(指向void的指针)代替char *作为通用指针的类型。