和实现有关的相同字符串存储方式检验的思考,该怎么处理
和实现有关的相同字符串存储方式检验的思考
/*Copyright (c) 2007,九天雁翎
* All rights reserved.
* 和实现有关的相同字符串存储方式检验的思考
* 完成日期:年月日*/
#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;
}
是Bjarne Stroustrup 错了?还是VC++2005在实现上的问题?我用了DEVC++效果也一样。假如是我错了,应该是我对指针地址的获得的错误,不过一个C字符串的指针地址应该怎么获得呢?
------解决方案--------------------
char *p1 = "Hello the world! ";
char *p2 = "Hello the world! ";
-------------------------
p1, p2是两个不同的指针变量,所以他们的地址值不同等,不过他们都指向同一个字符串常量 "Hello the world! "。
------解决方案--------------------
#include <iostream>
using namespace std;
int main()
{
char *p1 = "Hello the world! ";
char *p2 = "Hello the world! ";
//输出: 指针p1, p2的地址: 不同
cout < <&p1 < < " " < <&p2 < <endl;
//输出:指针p1, p2的值(p1, p2指向的字符串的地址): 相同
cout < <(void*)p1 < < " " < <(void*)p2 < <endl;
/*Copyright (c) 2007,九天雁翎
* All rights reserved.
* 和实现有关的相同字符串存储方式检验的思考
* 完成日期:年月日*/
#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;
}
是Bjarne Stroustrup 错了?还是VC++2005在实现上的问题?我用了DEVC++效果也一样。假如是我错了,应该是我对指针地址的获得的错误,不过一个C字符串的指针地址应该怎么获得呢?
------解决方案--------------------
char *p1 = "Hello the world! ";
char *p2 = "Hello the world! ";
-------------------------
p1, p2是两个不同的指针变量,所以他们的地址值不同等,不过他们都指向同一个字符串常量 "Hello the world! "。
------解决方案--------------------
#include <iostream>
using namespace std;
int main()
{
char *p1 = "Hello the world! ";
char *p2 = "Hello the world! ";
//输出: 指针p1, p2的地址: 不同
cout < <&p1 < < " " < <&p2 < <endl;
//输出:指针p1, p2的值(p1, p2指向的字符串的地址): 相同
cout < <(void*)p1 < < " " < <(void*)p2 < <endl;