C程序设计进阶
原课程链接:C程序设计进阶 const vs. static const 限制指针实参的功能 指针常量: 不能修改指针所指向的地址。在定义时必须立即初始化。 示例: char* const ptr = &some_char; // ptr 是一个指向 char 的常量指针 *ptr = 'a'; // 合法,修改了 ptr 所指向的 char 的值 ptr = &another_char; // 错误,不能修改 ptr 的值 常量指针: 不能修改指针所指向地址的内容,但可以改变指针所指向的地址。 示例: const char* ptr; // ptr 是一个指向 char 的常量指针 char const* ptr; // 与上一行等效 *ptr = 'a'; // 错误,不能修改 ptr 所指向的 char 的值 ptr = &another_char; // 合法,可以改变 ptr 的值 static 在函数内部或类中声明的静态变量,其生命周期跨越整个程序执行过程(在循环中多次执行,每次循环的初始值继承了上一次循环的值),且只初始化一次。 string literal #include <iostream> using namespace std; int main() { // input arguments int k=0, m=0, n=0; cin>>k; cin>>m>>" ">>n; // wrong expression. } cin operator automatically skips any whitespace, including spaces and newlines. it’s illegal to use “ ” with cin, because it is string literal, cin could not hold string. ...