常量字符串和指针

时间:2022-04-26
本文章向大家介绍常量字符串和指针,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

为了节省内存,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 }