Escolar Documentos
Profissional Documentos
Cultura Documentos
1057-CH06
Then the second variable contains the address of the first variable. After that, *itempcan be used as m. This is
called "indirect reference".
Intuitively, '*' and '&' are inverse operations, and '*&m' is the same as 'm' if 'm' is an ordinary variable, while '&*p'
is the same as 'p' if 'p' is a pointer.
As a special case, pointers can be used to access files. See the program in FIGURE 6.2 as an example.
www.cis.temple.edu/~pwang/1057-PC/Lecture/Ch06.htm
1/5
5/6/13
1057-CH06
4. Scope of names
The scope of a name refers to the region of a program where a particular meaning of a name is visible or can be
referenced.
www.cis.temple.edu/~pwang/1057-PC/Lecture/Ch06.htm
2/5
5/6/13
1057-CH06
For example, in the program of FIGURE 6.9, the constant macros have a scope that begins at their definitions, and
continues to the end of the source file. The scope of a function name start from its prototype (if available) or
header, and continues to the end of the source file. Formal arguments are local to the body of the defining function.
If a "local" variable/function has the same name as a "global" one (such as the name "one" in the previous example),
the former is used whenever the name is used in the local scope. It is invalid for more than one local (or global)
name to be identical within the same scope.
Please note that scanfexpects addresses of variable to be passed after the format, so the address of (&) symbol
is used. In this example, since the first and the third arguments are pointers (addresses of variable) themselves, they
are directly passed.
Now we can see that since scanfmay get many values (one for each format specifier in the format string), all
arguments passed to it must be pointers. The return value of scanfis the number of items it successfully converts
and stores.
In general, if function f1passes an argument xto another function f2, there are the following situations:
If xis used in both f1and f2as a value, then xis directly passed.
If xis used in both f1and f2as a pointer, then xis directly passed.
If xis used in f1as a value, but in f2as a pointer, then &xis passed.
If xis used in f1as a pointer, but in f2as a value, then *xis passed.
3/5
5/6/13
1057-CH06
Implementation: FIGURE 6.15. Two functions in it are left as exercises. In the program, "stubs" are used as place
holders.
4/5
5/6/13
1057-CH06
called "top-down testing", which means to get the big picture right before filling in all the details.
To test a sub-function by itself is called "unit testing", which can be performed with a simple "driver function", which
calls the function to be tested with proper actual arguments.
The process of separately testing individual functions before inserting them into the whole program is called
"bottom-up testing". Tests of the entire systems are called "system integration tests".
Debugging tips:
1. Document parameters and variables, as well as functions.
2. Trace executions by displaying functions entered.
3. Trace executions by displaying input/output of each function.
www.cis.temple.edu/~pwang/1057-PC/Lecture/Ch06.htm
5/5