我写了下面的代码中的字符*数组保存并打印以下内容: band1.txt band2.txt ... band3.txt 的代码似乎是正确的,但什么是印在控制台上是很奇怪。如何打印常量字符数组?
代码:
const char ** current_band = new const char * [103];
stringstream sstm;
string str;
for (i=0;i<103;i++){
current_band[i] = new char[11];
}
for (i=0;i<103;i++){
sstm.str("");
sstm << "band" << i+1 << ".txt";
str = sstm.str();
current_band[i] = str.c_str();
cout << current_band[i] << endl;
cout << i << endl;
}
for (i=0;i<103;i++){
cout << current_band[i] << endl;
cout << i << endl;
}
控制台:
band1.txt
band2.txt
...
band103.txt
然后最后循环:
band103.txt
band102.txt
band103.txt
band102.txt
...
band102.txt
band103.txt
这怎么可能?
编辑:其实我想要的“带”来为char *为了调用想要这样的说法
你应该使用'std :: string'和'std :: vector'来代替C字符串和原始数组。 – crashmstr
该代码不应编译。例如,您错过了'i'的声明。 – Shoe
_what_可能性如何?你还没有告诉我们什么是错的 –