【《C和指针》阅读笔记】Chapter1 Introduction

Discription

本程序从标准输入读取文本并对其进行修改,然后把它写到标准输出。
程序首先读取一串列标号。这些标号成对出现,表示输入行的列范围。这串列标号以一个负值结尾,作为结束标志。剩余的输入行被程序读入并打印,然后输入行中被选中范围的字符串被提取出来并打印。
注意:每行第1列的列标号为零!


Source Program

/*

** This program reads input lines from the standard input and prints

** each input line, followed by just some portions of the lines, to

** the standard output.

**

** The first input is a list of column numbers, which ends with a

** negative number.  The column numbers are paired and specify

** ranges of columns from the input line that are to be printed.

** For example, 0 3 10 12 -1 indicates that only columns 0 through 3

** and columns 10 through 12 will be printed.

*/

#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';

}


 

Interpretation

  • EXIT_SUCCESSEXIT_FAILURE
     
      stdio.h定义了EXIT_SUCESSEXIT_FAILURE符号。
     
     
  • 关键字 const
int read_colunm_numbers(int columns[ ], int max);
int rearrange(char* output, char const* input, 
                   int n_columns, int const columns[ ]);

 
  rearrange函数中第2个和第4个参数被声明为const,这表示函数将不会修改函数调用者所传递的这两个参数。
 
  在C语言中,数组参数是以引用(reference) 形式进行传递的,也就是传址调用,而标量和常量则是按值(value) 传递的(分别类似于Pascal和Modula中的var参数和值参数)。
 
  在函数中对标量参数的任何修改都会在函数返回时丢失,因此,被调用函数无法修改调用函数以传值形式传递给它的参数。然而,当被调用函数修改数组参数的其中一个参数时,调用函数所传递的数组就会被实际地修改。

所以,代码中rearrange函数被调用后,形参数组output[ ]会被实际地修改,而形参数组input[ ]由于关键字const的作用其值保持不变。

 
 

  • gets函数
        /*
        ** 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;

  gets函数从标准输入读取一行文本并把它存储于作为参数传递给它的数组中。一行输入由一串字符组成,以一个换行符(newline)结尾。gets函数丢弃换行符,并在该行的末尾存储一个NUL字节1(一个NUL字节是指字节模式为全0的字节,类似’\0’这样的字符常量)。然后,gets函数返回一个非NULL值,表示该行已被成功读取2。当gets函数被调用但事实上不存在输入行时,它就返回NULL值,表示它到达了输入的末尾(文件尾)。

尽管C语言并不存在“string”数据类型,但在整个语言中,存在一项约定:
字符串就是一串以NUL字节结尾的字符。
NUL是作为字符串的终止符,它本身并不被看作是字符串的一部分。

  例如,

字符串常量:
“Hello”
在内存中占据6个字节的空间,按顺序分别是H、e、l、l、o和NUL。

 

  • scanf函数
        /*

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

  scanf函数接受几个参数,其中第1个参数事一个格式字符串,用于描述期望的输入类型。剩余几个参数都是变量,用于存储函数所读取的输入数据。
  scanf函数的返回值是函数成功转换并存储于参数中的值的个数。

由于scanf的实现原理,所有标量参数的前面必须加上一个“&”符号。
数组参数前面不需要加上“&”符号3
但是,数组参数中如果出现了下标引用,也就是说实际参数是数组中的某个特定元素,那么它的前面也必须加上“&”符号。

 

  • getchar函数
        /*

        ** Discard the rest of the line that contained the final

        ** number.

        */

        while ((ch = getchar()) != EOF && ch != '\n')

               ;

  当scanf函数对输入值进行转换时,它只读取需要读取的字符。这样,该输入行包含了最后一个值的剩余部分仍会留在那里,等待被读取。它可能只包含作为终止符的换行符,也可能包含其他字符。不论如何,while循环将读取并丢弃这些剩余字符,防止它们被解释为第1行数据。
 
下面讨论这个表达式:

    (ch = getchar() ) != EOF && ch != '\n'

  首先,getchar函数从标准输入读取一个字符并返回它的值。如果输入中不再存在任何字符,函数就会返回常量EOF(在stdio.h中定义),用于提示文件的结尾。

  getchar函数返回的值被赋给变量ch,然后把它与EOF进行比较。如果ch == EOF,整个表达式的值就为假,循环将终止。若非如此,再把ch与换行符进行比较,如果两者相等,循环也将终止。
  因此,只有当输入尚未到达文件尾并且输入的字符并非换行符时,表达式的值才是真的(循环将继续执行)。这样,这个循环就能剔除当前输入行最后的剩余字符。

为什么ch被声明为整型,而我们事实上需要它来读取字符?
答案是EOF是一个整型值,它的位数比字符类型要多,把ch声明为整型可以防止从输入读取的字符意外地被解释为EOF。但同时,这也意味着接收字符的ch必须足够大,足以容纳EOF,这就是ch使用整型值的原因。
字符只是小整型数而已,所以用一个整型变量容纳字符值并不会引起任何问题。

 
 

  • 数组指针型形参
/*

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

  当数组名作为实参时,传给函数的实际上是一个指向数组起始位置的指针,也就是数组在内存中的地址。正因为实际传递的是一个指针而不是一份数组的拷贝,才使数组名作为参数时具备了传址调用的语义。函数可以按照操纵指针的方式来操纵实参,也可以像使用数组名一样用下标来引用数组的元素。
  但是,由于它的传址调用语义,如果函数修改了形参数组的元素,它实际上将修改实参数组的对应元素。因此,例子程序把columns声明为const就有两个方面的作用。首先,它声明该函数的作者的意图是这个参数不能被修改。其次,它导致编译器去验证是否违背该意图。因此,这个函数的调用者不必担心例子程序中作为第4个参数传递给函数的数组中的元素会被修改。

 
 

  • strncpy函数
               /*

               ** Copy the relevant data.

               */

               strncpy(output + output_col, input + columns[col],

                       nchars);

               output_col += nchars;

  strncpy函数把选中的字符从输入行复制到输出行中可用的下一个位置。strncpy函数的前两个参数分别是目标字符串源字符串的地址。在这个调用中,目标字符串的位置是输出数组的起始地址向后偏移output_col列的地址,源字符串的位置则是输入数组地址向后偏移columns[col]个位置的地址。第3个参数制定需要赋值的字符数4。输出列计算器随后向后移动nchars个位置。
 

        }


        output[output_col] = '\0';

  循环结束之后,输出字符串将以一个NUL字符作为终止符。
 


  1. NUL是ASCII字符集中’\0’字符的名字,它的字节模式为全0。NULL是指一个其值为0的指针。它们都是整型值,其值也相同,所以它们可以互换使用。然而,你还是应该使用适当的常量,因为它能告诉阅读程序的人不仅使用0这个值,而且告诉他使用这个值的目的。 ↩︎

  2. 符号NULL在头文件stdio.h中定义。另一方面,并不存在预定义的符号NUL,所以如果你想使用它而不是字符常量’\0’,你必须自行定义。
     
      ↩︎

  3. 但是,即使你在它前面加上一个“&”也没有什么不对,所以如果你喜欢,也可以加上它。
     
      ↩︎

  4. 如果源字符串的字符数少于第3个参数指定的复制数量,目标字符串中剩余的字节将用NUL字节填充。 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值