c while循环将循环中的局部变量视为全局变量,为什么?
下面是一个小型的 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] = response
和 words[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您分配
words[words_added] = response
时,您会将response
的地址(而不是内容)复制到数组中。因此,在原始形式中,您的代码应该将第二个单词和每个后续单词视为重复项。当您使用tmp
时,代码会将每个新的response
与存储在words[](每个位置)中的先前
,然后将其复制到tmp
进行比较tmp
(如果它不是重复的)。因此,我怀疑您的代码会检测到紧随原始内容之后的重复项,但不会检测到在 2 个或更多单词之后出现的重复项。
words
数组包含 4 个指针,但尚未为这些指针分配内存。您需要为
words
数组的每个元素分配内存,然后将每个字符串复制到其中:然后一定要在程序末尾
释放
内存:When you assign
words[words_added] = response
you're copying the address (not the contents) ofresponse
into the array. So in the original form, your code should see the second word and every subsequent word as duplicates. When you usetmp
the code compares each newresponse
to the previoustmp
that was stored in (every location of)words[]
, then copies it intotmp
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:Then be sure to
free
the memory at the end of your program:你做错了 - 你指向一个指针数组 -
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
}