第一章:快速上手注意点

给出第一章的源代码,简要的述说一下注意事项(平时在编程中经常会忘记的)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define	MAX_COLS	20	/* max # of columns to process */
#define	MAX_INPUT	1000	/* max len of input & output lines */

int	read_column_numbers( int columns[], int max );
void	rearrange( char *output, char const *input,
	    int n_columns, int const columns[] );

int
main( void )
{
	int	n_columns;		/* # of columns to process */
	int	columns[MAX_COLS];	/* the columns to process */
	char	input[MAX_INPUT];	/* array for input line */
	char	output[MAX_INPUT];	/* array for output line */

	/*
	** Read the list of column numbers
	*/
	n_columns = read_column_numbers( columns, MAX_COLS );

	/*
	** Read, process and print the remaining lines of input.
	*/
	while( gets( input ) != NULL ){
		printf( "Original input : %s\n", input );
		rearrange( output, input, n_columns, columns );
		printf( "Rearranged line: %s\n", output );
	}

	return EXIT_SUCCESS;
}

/*
** Read the list of column numbers, ignoring any beyond the specified
** maximum.
*/
int
read_column_numbers( int columns[], int max )
{
	int	num = 0;
	int	ch;

	/*
	** Get the numbers, stopping at eof or when a number is < 0.
	*/
	while( num < max && scanf( "%d", &columns[num] ) == 1
	    && columns[num] >= 0 )
		num += 1;

	/*
	** Make sure we have an even number of inputs, as they are
	** supposed to be paired.
	*/
	if( num % 2 != 0 ){
		puts( "Last column number is not paired." );
		exit( EXIT_FAILURE );
	}

	/*
	** Discard the rest of the line that contained the final
	** number.
	*/
	while( (ch = getchar()) != EOF && ch != '\n' )
		;

	return num;
}

/*
** 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 n_columns, int const columns[] )
{
	int	col;		/* subscript for columns array */
	int	output_col;	/* output column counter */
	int	len;		/* 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 = columns[col + 1] - columns[col] + 1;

		/*
		** 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;

		/*
		** 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.
		*/
		strncpy( output + output_col, input + columns[col],
		    nchars );
		output_col += nchars;
	}

	output[output_col] = '\0';
}

第一就是:数组边界条件的判断:

if( columns[col] >= len ||
		    output_col == MAX_INPUT - 1 )
			break;
如果超过边界,后面访问就会访问非法内存报错,而且判断要放于条件最前,

第二getchar()返回的值给ch,ch不能为char类型,不然我们得到的char中必然有个EOF即使,我们是正确的输入。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值