和实现有关的相同字符串存储方式检验的思考及c-string字符串
来源:互联网 发布:全医药学大词典 mac 编辑:程序博客网 时间:2024/05/17 03:28
/*Copyright (c) 2007,九天雁翎
* All rights reserved.
* 和实现有关的相同字符串存储方式检验的思考
* 完成日期:2007年7月7日*/
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
char *p1 = "Hello the world!";
char *p2 = "Hello the world!";
if(p1 == p2)
{
//ok,We could know it is the same as Bjarne Stroustrup said in VC++2005
cout << "The same."<<endl;
}
else
{
cout << "Not the same."<<endl;
}
//But look at this
cout << &p1 <<'/t' <<&p2 <<endl;
//the adress output is not the same!
//Do you want to output it use p1 and p2?
//Let try;
cout << p1 <<'/t' << p2 <<endl;
//We can only get the string.
//Let reaffirm it
if(&p1 == &p2)
{
cout<<"The same." <<endl;
}
else
{
cout<<"Not the same." <<endl;
}
return 0;
}
&p其实得到的是指针的地址,而不是c-string “Hello word!”的地址,而cout又为char* 重载了一个不同与一般指针的输出方式,所以你要输出”Hello world!”,可以通过static_cast<void*>方式,强制转换到void*指针,这样cout就可以输出想要的结果,结果自然和Bjarne Stroustrup说的一样,微软也没有错。
如下:
/*Copyright (c) 2007,九天雁翎
* All rights reserved.
* 和实现有关的相同字符串存储方式检验的思考
* 完成日期:2007年7月7日*/
#include "stdafx.h"
#include "myself.h"
#include <iostream>
using namespace std;
int main()
{
char *p1 = "Hello the world!";
char *p2 = "Hello the world!";
if(p1 == p2)
{
cout << "The same."<<endl;
}
else
{
cout << "Not the same."<<endl;
}
//Yes,it is.
cout << static_cast<void*>(p1) <<'/t' <<static_cast<void*>(p2) <<endl;
return 0;
}
- 和实现有关的相同字符串存储方式检验的思考及c-string字符串
- C++string类型字符串的存储方式
- 字符串数组和字符串指针(有关于指针的思考)
- 字符串的存储方式
- C++string中有关字符串内容修改和替换的函数浅析
- C++:与字符串常量有关的几个重要概念string literal, string, C-style charater string
- c-风格字符串和string类型的字符串的异同
- 有关String 字符串的各种操作大全
- String字符串的创建方式
- java基本类型及String字符串的存储与比较
- String类型字符串修改的思考
- oc---------有关字符串的创建方式
- Java中字符串(String)的存储和赋值原理
- Java字符串(String)的存储和赋值原理
- Java中字符串(String)的存储和赋值原理
- Java中字符串(String)的存储和赋值原理
- c++string字符串的输出
- Json的定义、标准格式及json字符串检验
- JavaMail API简介
- 用javamail进行认证发信
- 如何判断客户端的浏览器类型和版本号
- 砸开百度与企业网生财(来自:alibaba商人论坛:侯伯斯 的帖子)
- 陈皓谈程序员的升级:职业规划就像软件工程
- 和实现有关的相同字符串存储方式检验的思考及c-string字符串
- 实习了,思考过
- “快乐宝宝”风波始末 (三)
- ~~
- 中国电子商务网站的通病:多而杂(转载自:中国管理传播网)
- ASP.NET三层架构模式详解(留言簿)
- ubuntu打造成MAC
- 在unix下远程管理windows
- 获取对话框大小