一关于指针的小疑点
一关于指针的小问题
#include <iostream>
#include <string>
using namespace std;
void main(void)
{
char* str1 = "abc ";
char* str2 = "abc ";
const char* str3 = "abcd ";
const char* str4 = "abcd ";
const char str5[] = "abce ";
const char str6[] = "abce ";
char str7[] = "ab ";
char str8[] = "ab ";
bool a = (str1 == str2);
bool b = (str3 == str4);
bool c = (str5 == str6);
bool d = (str7 == str8);
cout < < c < < endl;
cout < < a < < endl;
cout < < b < < endl;
cout < < d < < endl;
}
char* str = "abc "和char str[] = "abc "的区别再哪里?
------解决方案--------------------
char *是指向那个地址空间,而abcde这些常数是放在一个固定的内存空间的
而char str[]则是把abcde这些拷到自己的内存空间里
所以就和char *有本质的区别
在你另一个帖子里回答过了
#include <iostream>
#include <string>
using namespace std;
void main(void)
{
char* str1 = "abc ";
char* str2 = "abc ";
const char* str3 = "abcd ";
const char* str4 = "abcd ";
const char str5[] = "abce ";
const char str6[] = "abce ";
char str7[] = "ab ";
char str8[] = "ab ";
bool a = (str1 == str2);
bool b = (str3 == str4);
bool c = (str5 == str6);
bool d = (str7 == str8);
cout < < c < < endl;
cout < < a < < endl;
cout < < b < < endl;
cout < < d < < endl;
}
char* str = "abc "和char str[] = "abc "的区别再哪里?
------解决方案--------------------
char *是指向那个地址空间,而abcde这些常数是放在一个固定的内存空间的
而char str[]则是把abcde这些拷到自己的内存空间里
所以就和char *有本质的区别
在你另一个帖子里回答过了