Pointers On C
Instructor’s Guide
Pointers on C—Instructor´s Guide
i
Contents
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
A Quick Start ........................................................................................................
1
Basic Concepts ......................................................................................................
7
Data .......................................................................................................................
11
Statements ............................................................................................................. 15
Operators and Expressions .................................................................................... 23
Pointers .................................................................................................................. 29
Functions ............................................................................................................... 37
Arrays ....................................................................................................................
43
Strings, Characters, and Bytes .............................................................................. 55
Structures and Unions ........................................................................................... 69
Dynamic Memory Allocation ................................................................................ 75
Using Structures and Pointers ............................................................................... 79
Advanced Pointer Topics ...................................................................................... 87
The Preprocessor ................................................................................................... 93
Input/Output Functions .......................................................................................... 95
Standard Library .................................................................................................... 119
Classic Abstract Data Types ................................................................................. 129
Runtime Environment ........................................................................................... 145
1
A Quick Start
1.1 Questions
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.2 Programming Exercises
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 <stdio.h>
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 <stdio.h>
#include <stdlib.h>
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 <stdio.h>
#include <stdlib.h>
#define MAX_LEN
int
main( void )
{
char
int
char
int
1001
/* Buffer size for longest line */
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. If it is longer than the previous
** longest line, save this line.
*/
len = strlen( input );
if( len > longest_len ){
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
longest.c
6. The statements
/*
** Make sure we have an even number of inputs ...
*/
if( num % 2 != 0 ){
puts( "Last column number is not paired." );
评论