C 丢失指向指针引用的指针

发布于 2025-01-16 04:41:11 字数 846 浏览 0 评论 0原文

我能够找到最常见的字母,但是当尝试再次打印 **words 中的元素时,我给出了 \000 值。

我尝试将 head 存储到另一个变量中但似乎我仍然失去了所有参考。 我也尝试将它作为常量传递,但我仍然失去参考。 void findFrequentLetter(const char **words) 并用 const 修改了调用。我怎样才能做到这一点?

我知道问题出在第一个 for(while()) 循环中。

void findFrequentLetter(char const **words) {
   int array[255] = {0}; // initialize all elements to 0
   char str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   int i, max;
   for (int d = 0; d < nb_words; ++d) {
      while (**words != '\0'){
         ++array[**words];
         ++(*words);
      }
      ++words;
   }
// Find the letter that was used the most
   max = array[0];
   int index = 0;
   for (i = 0; str[i] != 0; i++) {
      if (array[str[i]] > max ) {
         max = array[str[i]];
         index = i;
      }
   }
}

Im able to find the most frequent letter, but when trying to print the elements in **words again, Im given \000 values.

I tried to store head into another variable but seems like I still lose all reference.
I also tried to pass it as a constant but I still lose reference. void findFrequentLetter(const char **words) and also modified the call with const. How can I make this work?

I know the problem is in the first for(while()) loop.

void findFrequentLetter(char const **words) {
   int array[255] = {0}; // initialize all elements to 0
   char str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   int i, max;
   for (int d = 0; d < nb_words; ++d) {
      while (**words != '\0'){
         ++array[**words];
         ++(*words);
      }
      ++words;
   }
// Find the letter that was used the most
   max = array[0];
   int index = 0;
   for (i = 0; str[i] != 0; i++) {
      if (array[str[i]] > max ) {
         max = array[str[i]];
         index = i;
      }
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傻比既视感 2025-01-23 04:41:11

这是错误的:

for (int d = 0; d < stats->mot_sans_doublons; ++d)
{
    while (**words != '\0')
    {
        ++array[**words];
        ++(*words); // <===== HERE
    }
    ++words;
}

您的代码被赋予了指针数组的基地址。这些指针中的每一个都引用一个字符串。上面的代码行使该数组中的指针前进......永久。底层指针数组使该数组中的每个指针永久走到各自字符串的末尾。

遍历这些字符串的正确方法是使用中间指针。请勿以任何方式修改words的内容。例如:

for (int d = 0; d < stats->mot_sans_doublons; ++d)
{
    for (const char *s = words[d]; *s; ++s)
        ++array[(unsigned char)*s];
}

我将剩下的代码是否“有效”留给您,但这就是为什么在调用频率计数器后,您的单词数组似乎“丢失了其引用”的原因。

This is wrong:

for (int d = 0; d < stats->mot_sans_doublons; ++d)
{
    while (**words != '\0')
    {
        ++array[**words];
        ++(*words); // <===== HERE
    }
    ++words;
}

Your code is given the base address to an array of pointers. Each of those pointers refers to a string. The line in question above advances the pointers in that array... permanently. The underlying array of pointers has each pointer within that array permanently walked to the end of their respective strings.

The correct way to walk those strings is by intermediate pointer. Do NOT modify the content of words in any way. For example:

for (int d = 0; d < stats->mot_sans_doublons; ++d)
{
    for (const char *s = words[d]; *s; ++s)
        ++array[(unsigned char)*s];
}

Whether the rest of your code 'works' I leave to you, but that is the reason why, after calling your frequency counter, your words array seems to have "lost its references".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文