美文网首页
The C Programming Language

The C Programming Language

作者: melouverrr | 来源:发表于2016-05-24 21:25 被阅读56次
    • 在binary search中,如果全部都是通过ptr来操作原数组,需要注意mid = low + (high - low) / 2.
      • 原因在于指针是不可以相加的。而substraction is legal,结果就是两个指针(往往是同一数组内的)的offset的差值。
    • 对于一个大小为n的数组s,访问它的&s[n](i.e. s+n) is legal,dereferencing it is illegal.然而对于s[-1]是完全illegal。
    • 不要认为一个struct的大小就是所有member大小之和。
      • 为了对齐的要求(内存中),there may be unnamed holes in a structure.
    struct {
        char c;
        int i;
    };
    

    需要8个bytes,而不是5个。sizeof works properly here.

    for (ptr = head; ptr != NULL; p=p->Next) {
    }
    

    是walk through linked list idiom.

    • (the ptr type)malloc(sizeof(whatever you want)).malloc的返回值是void,需要显式cast到想要的类型

    • 在C语言中,用-表示空格。printf中,%s的几种变体:%.*s(接参数), %.10s(经测试,只有precision小于字符串长度的时候才有效,没有'.'也无效), %-10s(改为左对齐(默认情况下是右对齐),字符不够用空格补), %15.10s(取10个字符,总共占15个字符长度,不够的用空格补足,right alignment), %-15.10s同上,left alignment.

    • sprintf作用是把一个字符串格式化然后放入一个buffer字符串中,之后就可以使用这个字符串,而不必自己进行对字符数组的操作,十分方便.(buffer must be big enough to receive the result)

    • 实现minprintf的tricky bit is how minprintf walks along the argument list when list doesn't even have a name.

    • scanf: except the 1st, the other arguments, each of which must be a pointer, indicate where the corresponding converted input should be stored.

    • scanf: returning 0 means the next input character does not match the first specification in the format string.The next call to scanf resumes searching immediately after the last character already converted.

    • sscanf: reads from a string instead of the standard input.return the items assigned value successfully.

    • scanf: an input field is defined as a string of non-while space characters; it extends either to the next while space or until the field width.(WHITE-SPACE characters are blank, tab, newline(\n), carriage return(return key), veritcal tab, formfeed(进纸页))

    • No & is used with array name since it is a pointer.

    • scanf skips over white space(blank, tabs, newlines, etc) as it looks for input values.

    • scanf: to read input whose format is not fixed, it is often best to read a line at a time, then pick it apart with sscanf.(nice stratage!)

    Example:

    while (getline(line, sizeof(line)) > 0) {
        if (sscanf(line, "%d %s %d", &day, monthname, &year) == 3) {
            printf("valid: %s\n", line);
        }else if (sscanf(line, "%d/%d/%d", &month, monthname, &year) == 3) {
            printf("valid: %s\n", line);
        }else {
            printf("invalid %s\n", line);
        }
    }
    
    • binsearch on K & R C:
    int binsearch(int x, int v[], int n) {
        int low, high, mid;
        low = 0;
        high = n - 1;
        while (low <= high){
            mid = (low + high)/2;
            if (x < v[mid]) {
                high = mid - 1;
            }else if (x > v[mid]) {
                low = mid + 1;
            }else {
                return mid;//Found match
            }
        }
        return -1;//no match
    }
    

    相关文章

      网友评论

          本文标题:The C Programming Language

          本文链接:https://www.haomeiwen.com/subject/lpgzrttx.html