这是我的程序。输出应为“a”字符序列,但出于某种原因,它不是。为什么?
#include <iostream>
using namespace std;
const int NAME_LENGTH = 16;
struct Record {
char hotel_name[NAME_LENGTH];
};
int main() {
int amount = 5;
for (int i = 0; i < amount; i++) {
Record * elementToBeAdded = new Record;
for (int j = 0; j < NAME_LENGTH; j++)
elementToBeAdded->hotel_name[i] = 'a';
elementToBeAdded->hotel_name[NAME_LENGTH-1] = '\0';
cout << "string-" << elementToBeAdded->hotel_name << "-\n\n";
}
}
最佳答案
因为你有错字
elementToBeAdded->hotel_name[i] = 'a';
你是说j,不是我。
关于c++ - 字符串初始化失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9932436/