C ++:指向char指针数组的Char指针指向char数组
C++: Char pointer to char pointer array to char array
我会尽量简短:
所以我有一个任务,我正在创建一个 'Wordlist' class。我将在其中存储单词列表。
这些是成员变量
class WordList
{ //...
unsigned int m_count; // Number of words currently in list
unsigned int m_max; // The total size of the list.
char** m_list; // The list storing the words
};
这是我的构造函数
WordList::WordList(const int max_words) {
if(max_words < 1){
m_list = nullptr;
m_max = 0;
m_count = 0;
}
else
m_list = new char*[max_words];
m_count = 0;
m_max = max_words;
for (int i = 0; i < max_words; i++) {
m_list[i] = new char[20];
}
}
这就是我开始发现问题的地方。
下面的 add 函数应该以 c 风格字符串的形式添加一个单词,该单词从 **char m_list
指向的字符指针数组中指向。
int WordList::add(const char word[]) {
if (m_count == 0 && m_list != nullptr ) {
strcpy (m_list[m_count], word);
m_count++;
return 0;
}
if (m_count < m_max) {
m_count++;
strcpy (m_list[m_count], word);
return 0;
}
if (m_count == m_max) {
m_count++;
m_max ++;
strcpy (m_list[m_count], word);
return 1;
}
if (strlen(word)==0) {
return -2;
}
if (m_list == nullptr ){
return -2;
}
else
return -2;
}
所以我遇到的问题是我的 * 显然在语法上不正确,因为我没有得到指向完整单词的 5 个指针的数组,而是将第一个字母保存到最终目标字符,但是它没有像我想要的那样复制所有内容。
我确信我没有将我的问题翻译成英语,但希望这是一个开始。谢谢!
我将如何调用我的添加函数的示例:
WordList *wordlist = new WordList(5);
wordlist->add("harry");
wordlist->add("ron");
wordlist->add("hermione");
并且它应该在指针数组的底部添加一个指向每个单词的指针
所以
cout << wordlist->m_list[0][2] << endl; // Expect 'r'
cout << wordlist->m_list[1] << endl; // Expect "ron"
相反我得到
r
仅打印出来
我看不出你使用 double-pointers 有什么问题。
不过还有其他问题:
- 在你的
WordList::add
中,你应该首先检查空词或空列表,并快速失败。此外,在您的代码中,如果单词为空 - 您已经添加了它并从该函数返回。
- 在
if (m_count < m_max)
块中,您 pre-increment m_count
,将一个元素留空并冒着进入最后一个条目的 out-of-bounds 的风险。
- 在
if (m_count == m_max) {
你肯定会 out-of-bounds
- 建议:将 pre-allocating 20 个字符的数组保留为
nullptr
;当你需要一个词时 - 使用 strdup(word);
会为你分配一个必需的 space。
- 至于你的
I am getting the first letter saved
- 我猜你没有检查正确...
问题是你加了第一个词:
if (m_count == 0 && m_list != nullptr ) {
strcpy (m_list[m_count], word);
m_count++;
return 0;
}
递增 m_count 所以现在 m_count 是 1。
然后你加上第二个字:
if (m_count < m_max) {
m_count++;
strcpy (m_list[m_count], word);
return 0;
}
在添加单词之前递增 m_count,因此第二个单词位于索引 2 并且索引 1 被完全跳过。
您需要在复制单词后始终递增计数,因为 m_count 基于 1 而数组基于 0。
我会尽量简短: 所以我有一个任务,我正在创建一个 'Wordlist' class。我将在其中存储单词列表。 这些是成员变量
class WordList
{ //...
unsigned int m_count; // Number of words currently in list
unsigned int m_max; // The total size of the list.
char** m_list; // The list storing the words
};
这是我的构造函数
WordList::WordList(const int max_words) {
if(max_words < 1){
m_list = nullptr;
m_max = 0;
m_count = 0;
}
else
m_list = new char*[max_words];
m_count = 0;
m_max = max_words;
for (int i = 0; i < max_words; i++) {
m_list[i] = new char[20];
}
}
这就是我开始发现问题的地方。
下面的 add 函数应该以 c 风格字符串的形式添加一个单词,该单词从 **char m_list
指向的字符指针数组中指向。
int WordList::add(const char word[]) {
if (m_count == 0 && m_list != nullptr ) {
strcpy (m_list[m_count], word);
m_count++;
return 0;
}
if (m_count < m_max) {
m_count++;
strcpy (m_list[m_count], word);
return 0;
}
if (m_count == m_max) {
m_count++;
m_max ++;
strcpy (m_list[m_count], word);
return 1;
}
if (strlen(word)==0) {
return -2;
}
if (m_list == nullptr ){
return -2;
}
else
return -2;
}
所以我遇到的问题是我的 * 显然在语法上不正确,因为我没有得到指向完整单词的 5 个指针的数组,而是将第一个字母保存到最终目标字符,但是它没有像我想要的那样复制所有内容。
我确信我没有将我的问题翻译成英语,但希望这是一个开始。谢谢!
我将如何调用我的添加函数的示例:
WordList *wordlist = new WordList(5);
wordlist->add("harry");
wordlist->add("ron");
wordlist->add("hermione");
并且它应该在指针数组的底部添加一个指向每个单词的指针 所以
cout << wordlist->m_list[0][2] << endl; // Expect 'r'
cout << wordlist->m_list[1] << endl; // Expect "ron"
相反我得到
r
仅打印出来
我看不出你使用 double-pointers 有什么问题。
不过还有其他问题:
- 在你的
WordList::add
中,你应该首先检查空词或空列表,并快速失败。此外,在您的代码中,如果单词为空 - 您已经添加了它并从该函数返回。 - 在
if (m_count < m_max)
块中,您 pre-incrementm_count
,将一个元素留空并冒着进入最后一个条目的 out-of-bounds 的风险。 - 在
if (m_count == m_max) {
你肯定会 out-of-bounds - 建议:将 pre-allocating 20 个字符的数组保留为
nullptr
;当你需要一个词时 - 使用strdup(word);
会为你分配一个必需的 space。 - 至于你的
I am getting the first letter saved
- 我猜你没有检查正确...
问题是你加了第一个词:
if (m_count == 0 && m_list != nullptr ) {
strcpy (m_list[m_count], word);
m_count++;
return 0;
}
递增 m_count 所以现在 m_count 是 1。
然后你加上第二个字:
if (m_count < m_max) {
m_count++;
strcpy (m_list[m_count], word);
return 0;
}
在添加单词之前递增 m_count,因此第二个单词位于索引 2 并且索引 1 被完全跳过。
您需要在复制单词后始终递增计数,因为 m_count 基于 1 而数组基于 0。