這是我的程序。輸出應該是一個'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";
}
}
然後你的輸出是什麼? – talnicolas 2012-03-29 19:52:08
+1包括一個簡短的完整測試用例。請參閱http://sscce.org – 2012-03-29 20:15:20