【问题标题】:C++: Char pointer to char pointer array to char arrayC ++:Char指针指向char指针数组到char数组
【发布时间】:2021-01-26 08:25:54
【问题描述】:

我会尽量简短: 所以我有一个任务,我正在创建一个“Wordlist”课程。我将在其中存储单词列表。 这些是成员变量

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 个指针数组,而是我将第一个字母保存到最终目标字符,但它不是复制我想要的所有内容。

我确定我没有将我的问题翻译成英语,但我希望这是一个开始。谢谢!

我将如何调用我的 add 函数的示例:

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

只打印出来

【问题讨论】:

  • 首先,作业是否允许您使用std::vector&lt;std::string&gt;,因为这将是一个更好的选择。其次,请告诉我们您是如何调用add() 方法的。
  • 为什么你认为你只收到第一个字母?您能否提供一个显示意外行为的minimal reproducible example?此外,除非您的作业由于某种原因不允许,否则请使用std::vector&lt;std::string&gt;
  • "我开始找问题"-->strcpy (m_list[m_count], word);strlen(word) &gt; 19会有问题。
  • 感谢您的快速回复,我将发布更多示例代码。不幸的是,我根本无法使用字符串库。我认为这样我们才能真正专注于操作指针。

标签: c++ class pointers char c-strings


【解决方案1】:

问题是你加了第一个词:

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 开始的。

【讨论】:

  • 哇,有时只需要另一只眼睛就能捕捉到你刚刚卡住的东西。这很有帮助!
【解决方案2】:

我认为您使用双指针没有任何问题。

还有其他问题:

  1. 在您的WordList::add 中,您应该首先检查空字或空列表,然后快速失败。此外,在您的代码中,如果该词为空 - 您已经添加它并从该函数返回。
  2. if (m_count &lt; m_max) 块中,您预先增加m_count,将一个元素留空,并有可能在最后一个条目上越界。
  3. if (m_count == m_max) { 你肯定会越界
  4. 建议:不要预先分配20个字符的数组,而是保留nullptr;当你需要一个词时 - 使用strdup(word);,它会为你分配所需的空间。
  5. 至于你的I am getting the first letter saved - 我猜你没有检查对...

【讨论】:

  • 非常感谢您的帮助!我现在正在查看,非常感谢。
  • @StoneCodeStawny - 是的,请参阅en.cppreference.com/w/c/experimental/dynamic/strdup
  • @StoneCodeStawny 您可以使用 realloc 调整数组大小:en.cppreference.com/w/c/memory/realloc。如果对此有新问题 - 是的,请创建一个新问题。
  • @StoneCodeStawny 回复:您在问题中的打印代码 - 正如我在上面的回答中所解释的,您正在跳过元素 [1],因为您的 m_count 增量不正确
  • @StoneCodeStawny reallocstrcpy 一样多 C++ :)
猜你喜欢
  • 2012-03-19
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
相关资源
最近更新 更多