c while循环将循环中的局部变量视为全局变量,为什么?

发布于 2025-01-07 23:42:20 字数 1287 浏览 0 评论 0原文

下面是一个小型的 C 应用程序。它会要求您输入一个单词。它不再询问何时获得四个独特的单词。但在下面所示的表单中,除非取消注释相关行,否则它将无法正常运行。

#include <stdio.h>
#include <string.h>

#define WORDS_COUNT 4

int main()
{
    char* words[WORDS_COUNT];

    int words_added = 0;
    while (words_added<WORDS_COUNT)
    {

        puts ("\n-------enter a word-------");

        char response[250];

        scanf("%s", response);

        int i;
        int duplicate_flag = 0;
        for (i=0; i < words_added; i++)
        {
            if (strcmp(words[i], response) == 0)
            {
                duplicate_flag = 1;
                break;
            };
        };

        if (duplicate_flag == 0)
        {
            //char tmp[250];
            //strcpy(tmp, response);
            words[words_added] = response; //words[words_added] = tmp;
            puts("that's new!");
            words_added ++;
        } else {
            puts("you've said that already...");
        };

    };
    return 0;
};

您可以看到,主要区别在于 words[words_added] = responsewords[words_added] = tmp 之间。

为什么 tmp 变量起作用,而 response 不起作用?

我猜测 response 每次迭代都会有完全相同的地址,而 tmp 每次迭代都会得到一个新地址。但为什么?但它们都是在同一个 while 循环中声明的???

below is a small C application. It will ask you for a word to input. It stops asking when has attained four unique words. But in the form shown below it won't run properly until you uncomment the relevant lines.

#include <stdio.h>
#include <string.h>

#define WORDS_COUNT 4

int main()
{
    char* words[WORDS_COUNT];

    int words_added = 0;
    while (words_added<WORDS_COUNT)
    {

        puts ("\n-------enter a word-------");

        char response[250];

        scanf("%s", response);

        int i;
        int duplicate_flag = 0;
        for (i=0; i < words_added; i++)
        {
            if (strcmp(words[i], response) == 0)
            {
                duplicate_flag = 1;
                break;
            };
        };

        if (duplicate_flag == 0)
        {
            //char tmp[250];
            //strcpy(tmp, response);
            words[words_added] = response; //words[words_added] = tmp;
            puts("that's new!");
            words_added ++;
        } else {
            puts("you've said that already...");
        };

    };
    return 0;
};

The major difference as you can see is between words[words_added] = response and words[words_added] = tmp.

Why would the tmp variable work and not the response?

I'm guessing that response will have the exact same address every iteration, and tmp will get a new address every iteration. but why? yet they were both declared in same the while loop???

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

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

发布评论

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

评论(2

2025-01-14 23:42:20

当您分配 words[words_added] = response 时,您会将 response 的地址(而不是内容)复制到数组中。因此,在原始形式中,您的代码应该将第二个单词和每个后续单词视为重复项。当您使用 tmp 时,代码会将每个新的 response 与存储在 words[](每个位置)中的先前 tmp 进行比较,然后将其复制到tmp(如果它不是重复的)。

因此,我怀疑您的代码会检测到紧随原始内容之后的重复项,但不会检测到在 2 个或更多单词之后出现的重复项。

words 数组包含 4 个指针,但尚未为这些指针分配内存。
您需要为 words 数组的每个元素分配内存,然后将每个字符串复制到其中:

if (duplicate_flag == 0)
{
  words[words_added++] = strdup(response); // allocates mem and copies the string
  puts("that's new!");
} else {
  ...
}

然后一定要在程序末尾释放内存:

for (i = 0; i < words_added; ++i) {
  free(words[i]);
}

When you assign words[words_added] = response you're copying the address (not the contents) of response into the array. So in the original form, your code should see the second word and every subsequent word as duplicates. When you use tmp the code compares each new response to the previous tmp that was stored in (every location of) words[], then copies it into tmp if it's not a duplicate.

So I suspect that your code will detect a duplicate that immediately follows the original, but not one that occurs 2 or more words later.

The words array contains 4 pointers, but no memory has been allocated to those pointers.
You need to allocate memory for each element of the words array, and then copy each string into it:

if (duplicate_flag == 0)
{
  words[words_added++] = strdup(response); // allocates mem and copies the string
  puts("that's new!");
} else {
  ...
}

Then be sure to free the memory at the end of your program:

for (i = 0; i < words_added; ++i) {
  free(words[i]);
}
不美如何 2025-01-14 23:42:20

你做错了 - 你指向一个指针数组 - words[words_added] - 指向每次迭代都会更改的变量 - response

你需要在每次迭代中为 words[words_added] 分配存储空间,然后再进行 strcpy:
strcpy(words[words_added], response);

PS 不要放右大括号后的分号 }

You're doing it wrong - you're pointing an array of pointers - words[words_added] - at a variable that changes on every iteration - response

You need to allocate storage for words[words_added] on each iteration, before you strcpy:
strcpy(words[words_added], response);

P.S. Don't put semi-colons after closing braces }

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