我的变量在一个for循环中重置,不确定为什么。

huangapple go评论98阅读模式
英文:

my variable resetting after one loop in a for loop, not sure why

问题

我一直在尝试创建一个for循环,其中i < j。变量J获取字符串的长度,以决定将发生多少次循环。这对于第一个循环有效,我已经进行了调试,如果我输入一个包含3个字符的字符串,J确实会获取值3。问题在于,在第一个循环之后,J被重置为0,我似乎无法弄清楚为什么会这样。有人可以帮助我吗?我仍然认为自己是一个初学者,所以关于代码的任何建议都会受到欢迎,即使不是关于我的具体问题。

  1. #include <ctype.h>
  2. #include <cs50.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. int main(void)
  7. {
  8. string prompt = get_string("给我一个单词: ");
  9. int j = strlen(prompt);
  10. char word[] = "";
  11. for (int i = 0; i < j; i++)
  12. {
  13. strcat(word, "p");
  14. }
  15. printf("%s\n", word);
  16. }
英文:

i have been trying to make a for loop where at i &lt; j. Variable J takes the length of a string to decide how many loops will happen. This works for the first loop, i have debugged and J does get the value of 3 if i put in a string of 3 characters. The problem is that after the first loop J is reset to the value of 0 and i cant seem to figure out why that is. Can anyone help me? Id still call myself a beginner so any advice on code would be appreciated even if its not about my specific question.

  1. #include &lt;ctype.h&gt;
  2. #include &lt;cs50.h&gt;
  3. #include &lt;stdio.h&gt;
  4. #include &lt;string.h&gt;
  5. #include &lt;stdlib.h&gt;
  6. int main(void)
  7. {
  8. string prompt = get_string(&quot;Give me a word: &quot; );
  9. int j = strlen(prompt);
  10. char word[] = &quot;&quot;;
  11. for (int i = 0; i &lt; j; i++)
  12. {
  13. strcat(word, &quot;p&quot;);
  14. }
  15. printf(&quot;%s\n&quot;, word);
  16. }

答案1

得分: 3

  1. char word[] = &quot;&quot;; // 这里我认为是拼写错误,应该是
  2. char word[] = &quot;&quot;;
  3. You define a `char` array having one element (null terminating character).
  4. When you `strcat(word, &quot;p&quot;);` you invoke **undefined behaviour** as you write outside the array bounds. Your array has to be big enough to accommodate all the characters and null terminating character.

int main(void)
{
string prompt = get_string("Give me a word: " );
size_t j = strlen(prompt);
char word[j + 1];

  1. word[0] = &#39;
    word[0] = &#39;\0&#39;;
  2. for (int i = 0; j != 0 &amp;&amp; i &lt; j; i++)
  3. {
  4. strcat(word, &quot;p&quot;);
  5. }
  6. printf(&quot;%s\n&quot;, word);
  7. &#39;;
  8. for (int i = 0; j != 0 &amp;&amp; i &lt; j; i++)
  9. {
  10. strcat(word, &quot;p&quot;);
  11. }
  12. printf(&quot;%s\n&quot;, word);

}

  1. https://godbolt.org/z/5GPPse96K
英文:

char word\[\] = &quot;&quot;; I belive that it is typo and it should be

  1. char word[] = &quot;&quot;;

You define a char array having one element (null terminating character).
When you strcat(word, &quot;p&quot;); you invoke undefined behaviour as you write outside the array bounds. Your array has to be big enough to accommodate all the characters and null terminating character.

  1. int main(void)
  2. {
  3. string prompt = get_string(&quot;Give me a word: &quot; );
  4. size_t j = strlen(prompt);
  5. char word[j + 1];
  6. word[0] = &#39;
    int main(void)
  7. {
  8. string prompt = get_string(&quot;Give me a word: &quot; );
  9. size_t j = strlen(prompt);
  10. char word[j + 1];
  11. word[0] = &#39;\0&#39;;
  12. for (int i = 0; j != 0 &amp;&amp; i &lt; j; i++)
  13. {
  14. strcat(word, &quot;p&quot;);
  15. }
  16. printf(&quot;%s\n&quot;, word);
  17. }
  18. &#39;;
  19. for (int i = 0; j != 0 &amp;&amp; i &lt; j; i++)
  20. {
  21. strcat(word, &quot;p&quot;);
  22. }
  23. printf(&quot;%s\n&quot;, word);
  24. }

https://godbolt.org/z/5GPPse96K

huangapple
  • 本文由 发表于 2023年7月31日 20:21:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定