CPP-基础:strcpy之于C++(

以下对strcpy函数错误的是?

 1 char atr1[]="string";
 2 
 3 char str2[10];
 4 
 5 char *str3;
 6 
 7 char *str4="sting";
 8 
 9 
10 A.strcpy(str1,"hello");
11 B.strcpy(str2,"hello");
12 C.strcpy(str3,"hello");
13 D.strcpy(str4,"hello");

 

C.strcpy(str3,"hello"); 是不合法的。
开始str3没有指向任何空间。如果空间很多是空白的话,可能会没什么。但是有很多程序运行时,这样操作是很危险的,可能篡改其他数据。


D要分情况:
对于C语言,这种操作可以。对于c++,文字常量只能引用,不能修改。这种操作禁止。

 

附录:

使用#define定义常量
使用const定义常量
const unsigned short int studentPerclass = 15;//声明一个名为studentPerclass的符号常量,类型为unsigned short int,这种声明常量的方法与#define最大的不同在于该常量有类型,使得编译器能够根据类型确保它被正确使用

posted @ 2013-08-15 17:24  CPYER  阅读(467)  评论(0编辑  收藏  举报