Skip to content

Latest commit

 

History

History
96 lines (78 loc) · 2.52 KB

13.md

File metadata and controls

96 lines (78 loc) · 2.52 KB

C 编程,复习题

原文:Processes, Part 1: Introduction

校验:_stund

自豪地采用谷歌翻译

话题

  • C 字符串表示
  • C 字符串作为指针
  • char p [] vs char * p
  • 简单的 C 字符串函数(strcmp,strcat,strcpy)
  • sizeof char
  • sizeof x vs x *
  • 堆内存寿命
  • 调用堆分配
  • 取消引用指针
  • 操作符的地址
  • 指针算术
  • 字符串重复
  • 字符串截断
  • 双重释放错误
  • 字符串文字
  • 格式化打印
  • 内存超出界限错误
  • 静态内存
  • 文件io POSIX v C 库
  • C io fprintf 和 printf
  • POSIX 文件 io(读|写|打开)
  • stdout的缓冲

问题/练习

  • 以下是什么打印出来的
int main(){
    fprintf(stderr, "Hello ");
    fprintf(stdout, "It's a small ");
    fprintf(stderr, "World\n");
    fprintf(stdout, "place\n");
    return 0;
}
  • 以下两个声明之间有什么区别?其中一个sizeof会返回什么?
char str1[] = "bhuvan";
char *str2 = "another one";
  • c 中的字符串是什么?
  • 编码一个简单的my_strcmpmy_strcatmy_strcpymy_strdup怎么样?额外奖励:编写的函数仅需遍历一遍字符串。
  • 以下通常应该返回什么?
int *ptr;
sizeof(ptr);
sizeof(*ptr);
  • 什么是malloc?它与calloc有何不同?一旦内存被malloc编辑,我该如何使用realloc
  • 什么是&运算符? *怎么样?
  • 指针算术。假设以下地址。有以下几种变化?
char** ptr = malloc(10); //0x100
ptr[0] = malloc(20); //0x200
ptr[1] = malloc(20); //0x300
 * `ptr + 2`
 * `ptr + 4`
 * `ptr[0] + 4`
 * `ptr[1] + 2000`
 * `*((int)(ptr + 1)) + 3` 
  • 我们如何防止双重释放错误?
  • 什么是打印字符串,intchar的 printf 说明符?
  • 以下代码是否有效?如果是这样,为什么? output在哪里?
char *foo(int var){
    static char output[20];
    snprintf(output, 20, "%d", var);
    return output;
}
  • 编写一个函数,该函数接受一个字符串并打开该文件,第一次打印出文件的40个字节,但其他每次打印都会反转该字符串(请尝试使用POSIX API)
  • POSIX filedescriptor 模型和 C FILE*之间有什么区别(即使用了哪些函数调用,哪些是缓冲的)? POSIX 是否在内部使用 C FILE*,反之亦然?