《C和指针》学习笔记[第一章 快速上手]
好几次想看一些操作系统相关的书籍,但很多C语言的代码看着还是比较陌生,废话不多说。开干
书中示例代码:
// // main.c // pointers_on_c // // Created by sidian on 2022/5/25. // #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_COLS 20 /* 定义常量 */ #define MAX_INPUT 1000 int read_column_numbers(int colnums[], int max); /* 函数的声明 */ void rearrange(char *output, char const *input, int n_columns, int const columns[]); int main(void){ int n_columns; /* 需要处理的列数 */ int columns[MAX_COLS]; /* 要处理的列数的参数保存在数组中*/ char input[MAX_INPUT]; char output[MAX_INPUT]; n_columns = read_column_numbers(columns, MAX_COLS); 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; } /* **读取处理列的参数,保存到columns数组中 **返回需要处理的列数 */ int read_column_numbers(int columns[], int max){ int num = 0; int ch; /* 读取标准版输入数据,每一个数字代表需要处理的参数,当输入为负数截止*/ while( num < max && scanf("%d", &columns[num]) == 1 && columns[num] >= 0) num += 1; if (num % 2!=0) { puts("Last colum number is not paired."); /* 需要处理的列参数必须成对出现 */ exit(EXIT_FAILURE); } while ((ch=getchar()) != EOF && ch != '\n') /* 销毁输入-1之后,换行之前的字符*/ ; return num; } // 处理输入字符,拷贝到输出字符数组output void rearrange(char *output, char const *input, int n_columns, int const columns[]){ int col; int output_col; int len; len = strlen(input); output_col = 0; for (col = 0; col < n_columns; col += 2) { int nchars = columns[col + 1] - columns[col] + 1; /* 读取一对参数,计算需要剪切的字符个数 */ if (columns[col] >= len || /* 如果起始数已经超过输入字符产孤独,或者大于字符数组长度-1,终止 */ output_col == MAX_INPUT - 1) break; if (output_col + nchars > MAX_INPUT - 1) { /* 如果起始字符加上需要剪切字符长度超过数组长度-1, 调整剪切字符刚好可以填满字符数组长度-1*/ nchars = MAX_INPUT - output_col - 1; } strncpy(output + output_col, input + columns[col], nchars); output_col += nchars; } output[output_col] = '\0'; }
1.7 问题
1答:我的想法是,遵循一定的空白使用规则,看过清爽舒服,结构层次好
2答: 这样可以避免重复声明的情况,方便后续的维护。
3答: 使用#define定义字面值常量,在项目的使用中后续修改该参数非常方便,而且使整个代码的可读性更好
4答:
#include <stdio.h> #include "t_c.h" int main(void) { int n1 = 100; char *str= "杭州"; double f1 = 3.33; printf("%d %s %g\n", n1, str, f1); return 0; }
5:
#include <stdio.h> #include "t_c.h" int main(void) { int quantity, price; char department[200]; scanf("%d %d %s", &quantity,&price, department); printf("quantity is %d, price is %d, department is \"%s\"\n", quantity, price, department); return 0; }
6答: 因为C语言是一门强调新人程序员的语言,很好,很强大,我喜欢。
7答:如果不指定长度用strcpy我感觉可能会造成被修改的字符数组溢出数据
8答:接收的参数input可能会超出接收长度,导致数据溢出
1.8 编程练习
1:
#include <stdio.h> #include "t_c.h" int main(void) { char *str = "Hello World"; printf("%s\n", str); return 0; }
2:本题作弊了
#include <stdio.h> #include <stdlib.h> #include "t_c.h" int main(void) { int ch; int line; int at_beginning; line = 0; at_beginning = 1; while ((ch=getchar()) != EOF) { if ( at_beginning == 1) { at_beginning =0; line += 1; printf("%d ", line); } putchar(ch); if (ch == '\n') { at_beginning = 1; } } return EXIT_SUCCESS; }
3:
int main(void) { int ch; signed char checksum = -1; while ((ch=getchar()) != EOF) { putchar(ch); checksum += (signed char) ch; if (ch == '\n') { printf("%d\n", checksum); checksum = -1; } } return EXIT_SUCCESS; }
4.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "t_c.h" #define MAX_CHARS 1000 int main(void) { char in_string[MAX_CHARS]; char s_string[MAX_CHARS]; int s_len; int max_len = 0; while (gets(in_string) != NULL) { s_len = strlen(in_string); if (s_len > max_len) { strcpy(s_string, in_string); max_len = s_len; } } puts(s_string); return EXIT_SUCCESS; }
5.
void rearrange(char *output, char const *input, int n_columns, int const columns[]){ int col; int output_col; int len; len = strlen(input); output_col = 0; for (col = 0; col < n_columns; col += 2) { int nchars = columns[col + 1] - columns[col] + 1; /* 读取一对参数,计算需要剪切的字符个数 */ if (columns[col] >= len) continue; if (output_col == MAX_INPUT - 1) break; if (output_col + nchars > MAX_INPUT - 1) { /* 如果起始字符加上需要剪切字符长度超过数组长度-1, 调整剪切字符刚好可以填满字符数组长度-1*/ nchars = MAX_INPUT - output_col - 1; } strncpy(output + output_col, input + columns[col], nchars); output_col += nchars; } output[output_col] = '\0'; }
6.
int read_column_numbers(int columns[], int max){ int num = 0; int ch; /* 读取标准版输入数据,每一个数字代表需要处理的参数,当输入为负数截止*/ while( num < max && scanf("%d", &columns[num]) == 1 && columns[num] >= 0) num += 1; if (num % 2!=0) { columns[num] = MAX_INPUT; num += 1; } while ((ch=getchar()) != EOF && ch != '\n') /* 销毁输入-1之后,换行之前的字符*/ ; return num; }