内存分配虽然成功,但是尚未初始化就引用它

内存分配虽然成功,但是尚未初始化就引用它。 犯这种错误主要有两个起因:一是没有初始化的观念;二是误以为内存的缺省初值 全为零,导致引用初值错误(例如数组)。

内存的缺省初值究竟是什么并没有统一的标准,尽管有些时候为零值,我们宁可信其无不可信其有。

所以无论用何种方式创建数组,都别忘了赋初值,即便是赋零值也不 可省略,不要嫌麻烦。

 1 #include <iostream>
 2 #include <string.h>
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 int main(int argc, char** argv) {
 6      //声明字符数组和字符型指针变量
 7     char string[80],*p;
 8 
 9     //拷贝字符串
10     strcpy( string, "I'll see you");
11     cout<<"string:"<<string<<endl;
12 
13     //追加字符串
14     p=strcat( string, " in the morning.");
15     cout<<"String: "<<string<<endl;
16     cout<<"p     : "<<p<<endl;
17     return 0;
18 }
原文地址:https://www.cnblogs.com/borter/p/9413655.html