typedef或者using设置类型别名时注意const修饰之后变量的真正含义 & 变量初始化阶段
当substring设置为char*的别名
typedef char *substring;
那么下面两个变量含义不同
const substring b; //常量指针
const char * c; //指向常量的指针
#include<bits/stdc++.h>
using namespace std;
typedef char *substring;
int main()
{
substring a;
char res[4]={'a','b','c','\0'};
char res2[4]={'q','w','e','\0'};
cout << sizeof(char) << " " << sizeof(char *) << " " << sizeof(a) << endl;
const substring b=res;
const char * c=res;
cout << b << " " << c <<endl;
//测试是否可修改
b[0]='d';
//c[0]='e'; 报错,证明c是一个指向常量的指针
cout << b << " " << c <<endl;
//b=res2; //报错,证明b是一个常量指针
c=res2;
cout <<c <<endl;
}
变量初始化阶段
静态变量 全局变量(extern外部变量)和常量(const)
内存分配和初始化都是在编译阶段完成。
其他变量
编译阶段进行内存分配,运行阶段初始化。
变量作为数组长度
int x = 1; int array[x] = {0}; // 出错变量作为数组长度时,不能同时进行初始化
int x = 1; int array[x]; // 正确原文:https://blog.csdn.net/void_xinyue/article/details/105171452