C++ 使用指向字符的指针打印字符数组。

C++ 使用指向字符的指针打印字符数组。,c++,C++,在main.cpp中,我必须按如下方式打印我的字符数组: const char *str(CValue::TestStringValue0()); cout << ' ' << *str << endl; //must not change const char *tempStr(CValue::TestStringValue0()); const char **str = &tempStr; cout << ' ' << *

在main.cpp中,我必须按如下方式打印我的字符数组:

const char *str(CValue::TestStringValue0());
cout << ' ' << *str << endl; //must not change
const char *tempStr(CValue::TestStringValue0());
const char **str = &tempStr;
cout << ' ' << *str << endl; //must not change

我不确定我是否正确理解您的意思,但如果您不应该修改这一行:

cout << ' ' << *str << endl; //must not change
cout
const char*str(CValue::TestStringValue0());
const char*p=str;
对于(;*str;++str)
{

你能展示一下
TestStringValue0()
的代码吗?你可以不返回例如
str
而返回
str+1
什么样的
CValue::TestStringValue0
你的问题是什么?现在我有了它:static const char*TestStringValue0(){return“0,0”}'我必须打印“0,0,0”,而不是“0”你是在问如何让打印单个字符的代码在不改变字符串的情况下神奇地打印字符串吗?没有明智的方法,也可能没有疯狂的方法。还是我误解了?你为什么需要一个临时变量?非常感谢,老兄!我正在做一些项目,在main.cpp中,我们正在进行一些测试,我们无法修改@Alewenka,不客气:)如果我的答案有帮助,如果你不介意的话,你可以把它标记为接受;)@andre,因为我们不能用
count
修改行,我们需要将
*str*设为指针,所以我们需要将
str`设为指向指针的指针。至于额外的变量,我想,我可以避免它,但我觉得不舒服获取刚从函数返回的值的地址:)这是一个习惯问题。我也考虑过:)一个不错的:)唯一的问题是
cout
结尾的愚蠢的
endl
:)@free昵称愚蠢的不是endl。愚蠢的是assignment:)
const char *tempStr(CValue::TestStringValue0());
const char **str = &tempStr;
cout << ' ' << *str << endl; //must not change
const char *str(CValue::TestStringValue0());
const char *p = str;

for ( ; *str; ++str )
{
    cout << ' ' << *str << endl; //must not change
}