(1)sizeof的返回值类型为size_t(unsigned int); (2)sizeof是运算符,而strlen是函数; (3)sizeof可以用类型做参数,其参数可以是任意类型的或者是变量、函数,而strlen只能用char*做参数,且必须是以’\0’结尾; (4)数组作sizeof的参数时不会退化为指针,而传递给strlen是就退化为指针; (5)sizeo是编译时的常...
inti=1;int*pi=&i; i自身是变量,通过指针pi也可以修改i的值,pi也可以再次指向其他变量 指向const变量的指针 constinti=1;constint*pi=&i; 判断一个变量的类型时,从变量名从右向左看,首先,*指示pi是一个指针,const int指示pi是一个指向const变量的指针。和引用类型一样,指向const变量的指针必须被声明为cons...
1constint*f1(){2int*p;3p =newint;4*p =1;5returnp;6}7intmain(){8constint*p1;9p1 =f1();10return0;11} 若第8行改为int *p1;则编译时报错:“[8] error: invalid conversion from 'const int*' to 'int*'” (编译器code::block); 若主函数改为: 7intmain(){8constint*p1;9p1 =f1...
1.const可以放在*的左边 2.const可以放在*的右边 1.const放在*的左边 Plain Text 复制代码 99 1 2 3 4 5 6 7 8 9 10 11 12 int main(){ const int a = 10;int const* p = &a; //限制的是*p //意思是不能通过p来修改p指向的空间的内容 //*p = 0;//err,报错 int b = 20;p =...
1:String类型 1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 //初始化方法 6 string s1 = "hello";//默认构造方法 7 string s2(s1);//将s2初始化为s1的一个副本 8 string s3("value");//将s3初始化为字符串的副本 ...
A(int size) : SIZE(size) {}; private: const int SIZE; }; int main() { A a(100); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 说明 在类中声明变量为const类型,但是不可以初始化 const常量的初始化必须在构造函数初始化列表中初始化,而不可以在构造函数函数体内初...
声明constexpr的变量一定是一个常量,而且必须用常量表达式初始化: constexpr int mf =20; // 20是常量表达式 constexpr int litter = mf+1; //mf+1 是常量表达式 constexpr int max= size(); //只有当size是一个constexpr函数时才是一条正确的声明语句。 不能使用普通函数作为constexpr变量的初始值。 一...
size_tstrlen(constchar* str); intstrcmp(constchar* str1,constchar* str2); char*strcat(char* destination,constchar* source); char*strcpy(char* destination,constchar* source); intsystem(constchar* command); intputs(constchar* str);
size_tstrlen(constchar* str); intstrcmp(constchar* str1,constchar* str2); char*strcat(char* destination,constchar* source); char*strcpy(char* destination,constchar* source); intsystem(constchar* command); intputs(constchar* str);
\"is ";constN<countlower("Hello, world!")>out2;// implicitly converted to conststrconstexprinta[12]={0,1,2,3,4,5,6,7,8};constexprintlength_a=sizeof(a)/sizeof(int);// std::size(a) in C++17,// std::ssize(a) in C++20std::cout<<"array of length "<<length_a<<" has...