Pointers On C
Instructor’s Guide
###E###
Pointers on C—Instructor´s Guide
i
Contents
1
A Quick Start ........................................................................................................
Chapter 1
7
Basic Concepts ......................................................................................................
Chapter 2
11
Data .......................................................................................................................
Chapter 3
15
Statements .............................................................................................................
Chapter 4
23
Operators and Expressions ....................................................................................
Chapter 5
29
Pointers ..................................................................................................................
Chapter 6
37
Functions ...............................................................................................................
Chapter 7
43
Arrays ....................................................................................................................
Chapter 8
55
Chapter 9
Strings, Characters, and Bytes ..............................................................................
69
Chapter 10 Structures and Unions ...........................................................................................
75
Chapter 11 Dynamic Memory Allocation ................................................................................
79
Chapter 12 Using Structures and Pointers ...............................................................................
87
Chapter 13 Advanced Pointer Topics ......................................................................................
93
Chapter 14 The Preprocessor ...................................................................................................
Chapter 15 Input/Output Functions ..........................................................................................
95
Chapter 16 Standard Library .................................................................................................... 119
Chapter 17 Classic Abstract Data Types ................................................................................. 129
Chapter 18 Runtime Environment ........................................................................................... 145
###E###
1
A Quick Start
1.1Questions
1. To make the program easier to read, which in turn makes it easier to maintain later.
3.
It is easier to see what a named constant represents, if it is well named, than a literal constant,
which merely displays its value.
4. "%d %s %g\n"
6. The programmer can put in subscript checks where they are needed; in places where the sub-
script is already known to be correct (for example, from having been checked earlier), there is no
overhead expended in checking it again. But the real reason they are omitted is the fact that sub-
scripts are implemented as pointer expressions, which are described in Chapter 8.
7. More characters would be copied than are actually needed; however, the output_col would be
updated properly, so the next range of characters would be copied into the output array at the
proper place, replacing any extra characters from the preceding operation. The only potential
problem is that the unbounded strcpy might copy more characters into the output array than it
has room to hold, destroying some other variables.
1.2ProgrammingExercises
1. Watch the solutions for proper use of void declarations and a reasonable style. The first pro-
gram is no place to begin learning bad habits. The program will compile and run on most sys-
tems without the #include statement.
/*
** Print the message "Hello world!" to the standard output.
*/
#include
void
main( void )
Solution 1.1
continued...
1
2
Chapter 1 A Quick Start
{
}
printf( "Hello world!\n" );
Solution 1.1
hello_w.c
3. Many students will attempt to read the input file line by line, which is unnecessarily complicated.
Other common errors are to forget to initialize the sum to -1, or to declare it an integer rather
than a character. Finally, be sure the variable used to read the characters is an integer; if it is a
character variable, the program will stop on systems with signed characters when the input con-
tains the binary value 0377 (which, when promoted to an integer, is -1 and equal to EOF). Note
that the overflow renders this program nonportable, but we don’t know enough yet to avoid it.
/*
** This program copies its standard input to the standard output, and computes
** a checksum of the characters.
*/
The checksum is printed after the input.
#include
#include
int
main( void )
{
int
char
c;
sum = –1;
/*
** Read the characters one by one, and add them to the sum.
*/
while( (c = getchar()) != EOF ){
putchar( c );
sum += c;
}
printf( "%d\n", sum );
return EXIT_SUCCESS;
}
Solution 1.3
checksum.c
4. The basis of this program is an array which holds the longest string found so far, but a second
array is required to read each line. The buffers are declared 1001 characters long to hold the
data plus its terminating NUL byte. The only tricky thing is the initialization to prevent garbage
from being printed when the input is empty.
/*
** Reads lines of input from the standard input and prints the longest line that
** was found to the standard output.
It is assumed that no line will exceed
Solution 1.4
continued...
Pointers on C—Instructor´s Guide
3
** 1000 characters.
*/
#include
#include
#define MAX_LEN
1001
/* Buffer size for longest line */
int
main( void )
{
char
int
char
int
input[ MAX_LEN ];
len;
longest[ MAX_LEN ];
longest_len;
/*
** Initialize length of the longest line found so far.
*/
longest_len = –1;
/*
** Read input lines, one by one.
*/
while( gets( input ) != NULL ){
/*
** Get length of this line.
** longest line, save this line.
*/
len = strlen( input );
if( len > longest_len ){
If it is longer than the previous
longest_len = len;
strcpy( longest, input );
}
}
/*
** If we saved any line at all from the input, print it now.
*/
if( longest_len >= 0 )
puts( longest );
return EXIT_SUCCESS;
}
Solution 1.4
6. The statements
/*
** Make sure we have an even number of inputs ...
*/
if( num % 2 != 0 ){
puts( "Last column number is not paired." );
longest.c
4
Chapter 1 A Quick Start
exit( EXIT_FAILURE );
}
are removed from the read_column_numbers function, and the rearrange function is
modified as follows. Note that the computation of nchars is moved after the test that checks
whether the starting column is within the bounds of the input string.
/*
** Process a line of input by concatenating the characters from the indicated
** columns. The output line is then NUL terminated.
*/
void
rearrange( char *output, char const *input, int const n_columns,
int const columns[] )
{
int
int
int
col;
output_col;
len;
/* subscript for columns array */
/* output column counter */
/* length of input line */
len = strlen( input );
output_col = 0;
/*
** Process each pair of column numbers.
*/
for( col = 0; col < n_columns; col += 2 ){
int
nchars;
/*
** If the input line isn’t this long or the output array is
** full, we’re done.
*/
if( columns[col] >= len || output_col == MAX_INPUT – 1 )
break;
/*
** Compute how many characters to copy.
*/
if( col + 1 < n_columns )
nchars = columns[col + 1] – columns[col] + 1;
else
nchars = len – columns[col + 1];
/*
** If there isn’t room in the output array, only copy what will
** fit.
*/
if( output_col + nchars > MAX_INPUT – 1 )
nchars = MAX_INPUT – output_col – 1;
/*
** Copy the relevant data.
Solution 1.6
continued...