热搜关键词: 电路基础ADC数字信号处理封装库PLC

pdf

c和指针课后题答案(完整版)

  • 1星
  • 2016-02-18
  • 298.92KB
  • 需要1积分
  • 100+ 次下载
标签: C和指针

C和指针

课后答案

课后答案

C和指针的配套课后题答案  高清

文档内容节选

Pointers On C Instructors Guide Pointers on CInstructors 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 ......

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." );
展开预览

猜您喜欢

评论

liganchina
英文版的,可以参考
2022-04-19 17:00:46
登录/注册

意见反馈

求资源

回顶部

推荐内容

热门活动

热门器件

随便看看

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
×