2月27日终于下决心改变学习策略:
- 不再总结一些基础的知识
- 不会把书中的要点记录在博客,而是直接买书(之前都是图书馆借书),直接阅读书籍
- 博客主要总结难度较大的问题
这是最后一篇基础性的记录博客,但是很不完整,所以只是把题贴下来了,以后完成时会单开一篇博客。
5.2 指针与函数
Exercise 5-1. As written, getint treats a + or - not followed by a digit as a valid representation of zero. Fix it to push such a character back on the input.
答:
暂时未找到合适的解法。
Exercise 5-2. Write getfloat, the floating-point analog of getint. What type does getfloat return as its function value?
Exercise 5-2. Write getfloat, the floating-point analog of getint. What type does getfloat return as its function value?
答:
暂时未找到合适的解法。
5.5字符指针和函数
Exercise 5-3. Write a pointer version of the function strcat that we showed in Chapter 2: strcat(s,t) copies the string t to the end of s.
答:
暂时未找到合适的解法。
Exercise 5-4. Write the function strend(s,t), which returns 1 if the string t occurs at the end of the string s, and zero otherwise.
答:
暂时未找到合适的解法。
Exercise 5-5. Write versions of the library functions strncpy, strncat, and strncmp, which operate on at most the first n characters of their argument strings. For example, strncpy(s,t,n) copies at most n characters of t to s. Full descriptions are in Appendix B.
答:
暂时未找到合适的解法。
Exercise 5-6. Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include getline (Chapters 1 and 4), atoi, itoa, and their variants (Chapters 2, 3, and 4), reverse (Chapter 3), and strindex and getop (Chapter 4).
答: 暂时未找到合适的解法。
答: 暂时未找到合适的解法。
5.6 指针数组以及指向指针的指针
- unix有个程序sort,可以完成一个功能:根据每行字符串的第一个字符,按字典顺序排序。
- 本节试着写了一个例子,简化版的sort。
- 使用 指针数组 可以高效的完成这个功能,如下图所示:
由指针组成的数组,每一个指针指向一行。不用移动文本行,仅仅改变指针的指向,就完成了这个功能 - 移动文本行将带来复杂的储存管理和巨大的开销
- 在比较字符的时候我们使用了strcmp函数。
- 程序的划分:一个问题一个函数,主函数控制其他函数的执行。
- 具体的函数我倒没有觉得有什么难点,不过可以来看看指针数组的初始化,实际上也没什么区别:
char *lineptr[MAXLINES]
练习题
Exercise 5-7. Rewrite readlines to store lines in an array supplied by main, rather than calling alloc to maintain storage. How much faster is the program?答:code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
#define MAXLINES 5000 /* maximum number of lines */
#define MAXLEN 1000 /* maximum length of a line */
char *lineptr[MAXLINES];
char lines[MAXLINES][MAXLEN];//第一个相当于存入的一行的编号
/* K&R2 p29 */
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
s[i] = c;
if (c == '\n') {
s[i++] = c;
}
s[i] = '\0';
return i;
}
/* K&R2 p109 */
int readlines(char *lineptr[], int maxlines)
{
int len, nlines;
char *p, line[MAXLEN];
nlines = 0;
while ((len = getline(line, MAXLEN)) > 0)
if (nlines >= maxlines || (p = malloc(len)) == NULL)//Malloc 向系统申请分配指定size个字节的内存空间
return -1;
else {
line[len - 1] = '\0'; /* delete the newline */
strcpy(p, line);//将读到的行复制到新申请的内存空间中
lineptr[nlines++] = p;
}
return nlines;
}
int readlines2(char lines[][MAXLEN], int maxlines)
{
int len, nlines;
nlines = 0;
while ((len = getline(lines[nlines], MAXLEN)) > 0)
if (nlines >= maxlines)
return -1;
else
lines[nlines++][len - 1] = '\0';
return nlines;
}
int main(int argc, char *argv[])
{
/* read things into cache, to be fair. */
readlines2(lines, MAXLINES);
if (argc > 1 && *argv[1] == '2') {
puts("readlines2()");
readlines2(lines, MAXLINES);
} else {
puts("readlines()");
readlines(lineptr, MAXLINES);
}
return 0;
}
5.7 多维数组
- 类似矩阵的多维数组,使用不如指针数组那么频繁
- char类型在存放较小的非字符整数也是合法的
- 看一下二维数组的赋值,下面的二维数组表示某个月有多少天:
static char daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} };
- 看作矩阵的话:
第一个是行,第二个是列daytab[i][j] /* [row][col] */
- 在二位数组作为参数传递给函数的时候,在函数的声明中必须指名数组的列数,但是多少行可以不写。
第三种写法是因为,二维数组相当于储存了一个指向很多对象的指针,每个对象都是由13个整型元素构成的一维数组。具体说一下,就是一个数组在被调用的时候传递的就是指针。f(int daytab[2][13]) { ... }//可以这样 f(int daytab[][13]) { ... }//可以这样 f(int (*daytab)[13]) { ... }//也可以这样,小心圆括号,改变优先级
int *daytab[13]//[]的优先级高,所以这样表示一个指向整型的数组指针
- 一般多维数组中,第一维可以不指定大小,其余各维都必须明确大小
练习题
Exercise 5-8. There is no error checking in day_of_year or month_day. Remedy this defect.(进行错误检查。)