首页 > 技术文章 > 常量字符串和指针

bewolf 2015-03-09 10:52 原文

为了节省内存,C++把常量字符串单独放在一个内存区域,如果有几个指针指向相同的常量字符串时,它们实际上指向的是相同的内存地址。

而数组是要每一个数组单独占用一块内存的

 1 #include "stdafx.h"
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int _tmain(int argc, _TCHAR* argv[])
 6 {
 7     char str1[]="hello world";
 8     char str2[]="hello world";
 9     char *str3="hello world";
10     char *str4="hello world";
11     return 0;
12 }

推荐阅读