全国咨询热线123456789
strcpy
发布时间:2020-12-04 16:34浏览次数:
  • strcpy
  • strcpy

    //strcpy函数的使用
    
    #include<iOStream>
    using namespace std;
    int main(){
    	char a[7] = "abcdef"; //不能写成char a[6] = "abcdef";如果7改成6,编译时报错:ERROR: initializer-string for char array is too long
    	char b[4] = "ABC";    //不能写成char a[3] = "ABC";如果4改成3,编译时报错:error: initializer-string for char array is too long
    
    	cout<<"a[0]:"<<a[0]<<endl;
    	cout<<"a[1]:"<<a[1]<<endl;
    	cout<<"a[2]:"<<a[2]<<endl;
    	cout<<"a[3]:"<<a[3]<<endl;
    	cout<<"a[4]:"<<a[4]<<endl;
    	cout<<"a[5]:"<<a[5]<<endl;
    	cout<<"a[6]:"<<a[6]<<endl;
    	cout<<"a[7]:"<<a[7]<<endl;
    	cout<<"a[8]:"<<a[8]<<endl;
            cout<<"a:"<<a<<endl;
    	cout<<"========================"<<endl;
    	strcpy(a,b);
    	cout<<"strcpy(a,b);之后"<<endl;
    	cout<<"a[0]:"<<a[0]<<endl;
            cout<<"a[1]:"<<a[1]<<endl;
            cout<<"a[2]:"<<a[2]<<endl;
            cout<<"a[3]:"<<a[3]<<endl;
            cout<<"a[4]:"<<a[4]<<endl;
            cout<<"a[5]:"<<a[5]<<endl;
            cout<<"a[6]:"<<a[6]<<endl;
            cout<<"a[7]:"<<a[7]<<endl;
            cout<<"a[8]:"<<a[8]<<endl;
            cout<<"a:"<<a<<endl;
    	cout<<a[5]<<endl;
    	return 0;
    }
    
    //输出:
    /*******************
    a[0]:a
    a[1]:b
    a[2]:c
    a[3]:d
    a[4]:e
    a[5]:f
    a[6]:
    a[7]:
    a[8]:
    a:abcdef
    ========================
    strcpy(a,b);之后
    a[0]:A
    a[1]:B
    a[2]:C
    a[3]:
    a[4]:e
    a[5]:f
    a[6]:
    a[7]:
    a[8]:
    a:ABC
    f
    ******************/
    /*
    
    这个程序原本a中的数据是 "abcdef\0", b中的数据是"ABC\0"  strcpy(a,b); 把b中的字符串复制到a中, 所以a变成了"ABC\0ef\0" 所以你输出a[5], 就是f
    */
  • strcpy
  • -->