声明数组大小遮蔽了局部变量

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

declaring array size shadows a local variable

问题

我在CS50第2周的数组灯泡问题中。我有一个函数将ASCII值转换为8位二进制,然后将值存储在数组中,问题是,如果不声明数组的大小,会出现错误:无法初始化可变大小的对象,如果声明数组的大小,会出现错误:声明会遮蔽局部变量。我不知道该怎么办,显然是编程新手,任何帮助都不胜感激,以下是代码:

  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main(void)
  5. {
  6. char* message = get_string("message:");
  7. int i=0;
  8. int array[7]; //这是导致声明遮蔽局部变量的原因,还尝试过int array[]={0,0,0,0,0,0,0,0};
  9. //如果没有这行,下面会出现可变大小对象无法初始化的错误
  10. while (message[i] != '
    #include <cs50.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. int main(void)
  14. {
  15.     char* message = get_string("message:");
  16.     int i=0;
  17.     int array[7]; //这是导致声明遮蔽局部变量的原因,还尝试过int array[]={0,0,0,0,0,0,0,0};
  18.               //如果没有这行,下面会出现可变大小对象无法初始化的错误
  19.     while (message[i] != '\0')
  20.     {
  21.         int j= message[i]; //这将字符转换为ASCII
  22.         for (int h=7; h>=0; h--)
  23.         {
  24.             if (j % 2 == 1)
  25.             {
  26.                 array[h]=1;//如果在上面声明大小时不遮蔽这个,会出现可变大小对象无法初始化的错误
  27.                 j=(j/2); //我实际上不知道这是否会四舍五入到0.5,因为它是一个整数,但这真的不是我现在担心的主要问题
  28.             }
  29.             else
  30.             {
  31.                 array[h]=0; //代码的目的是将消息中的每个字符打印成8位二进制
  32.                 j=(j/2);
  33.             }
  34.             for (int y=0; y < 8; y++)
  35.             {
  36.                 printf("%i",array[y]); //我明白我正在遮蔽数组,但如果我将数组的名称更改为array1,那么它就是一个新的数组,会出现可变大小对象无法初始化的错误
  37.             }
  38.             printf("\n");
  39.         }
  40.         i++;
  41.     }
  42.     printf("\n");
  43. }
  44. ')
  45. {
  46. int j= message[i]; //这将字符转换为ASCII
  47. for (int h=7; h>=0; h--)
  48. {
  49. if (j % 2 == 1)
  50. {
  51. array[h]=1;//如果在上面声明大小时不遮蔽这个,会出现可变大小对象无法初始化的错误
  52. j=(j/2); //我实际上不知道这是否会四舍五入到0.5,因为它是一个整数,但这真的不是我现在担心的主要问题
  53. }
  54. else
  55. {
  56. array[h]=0; //代码的目的是将消息中的每个字符打印成8位二进制
  57. j=(j/2);
  58. }
  59. for (int y=0; y < 8; y++)
  60. {
  61. printf("%i",array[y]); //我明白我正在遮蔽数组,但如果我将数组的名称更改为array1,那么它就是一个新的数组,会出现可变大小对象无法初始化的错误
  62. }
  63. printf("\n");
  64. }
  65. i++;
  66. }
  67. printf("\n");
  68. }

所以我确定这段代码存在很多问题,它可以更好地优化,但我真正关心的是如何在不遮蔽局部变量的情况下声明数组的大小。另外,我不得不逐行重新编写所有这些代码,从Visual Studio中复制,所以这里可能有语法错误,这还没有完成,即使修复了数组错误,我仍然需要将其打印为灯泡表情而不是1和0。

英文:

I am in CS50 week 2 arrays bulbs problem
I have a function to turn ASCII value into 8 bit binary then store value in array the problem is without declaring the size of the array it gives error: Variable-sized object may not be initialized and if I declare the size of the array it gives error: declaration shadows local variable I don't know what to do obviously new to coding any and all help is appreciated here is the code:

  1. #include &lt;cs50.h&gt;
  2. #include &lt;stdio.h&gt;
  3. #include &lt;string.h&gt;
  4. int main(void)
  5. {
  6. char* message = get_string(&quot;message:&quot;);
  7. int i=0;
  8. int array[7]; //this is what gives declaration shadows local variable also have tried int array[]={0,0,0,0,0,0,0,0};
  9. //without this line I get the variable-sized object may not be initialized down bellow
  10. while (message[i] != &#39;
    #include &lt;cs50.h&gt;
  11. #include &lt;stdio.h&gt;
  12. #include &lt;string.h&gt;
  13. int main(void)
  14. {
  15. char* message = get_string(&quot;message:&quot;);
  16. int i=0;
  17. int array[7]; //this is what gives declaration shadows local variable also have tried int array[]={0,0,0,0,0,0,0,0};
  18. //without this line I get the variable-sized object may not be initialized down bellow
  19. while (message[i] != &#39;\0&#39;)
  20. {
  21. int j= message[i]; // this converts char to ASCII
  22. for ( h=7;h&gt;=0;h--)  
  23. {
  24. if (j % 2 == 1)
  25. {
  26. int array[h]=1;// if I don&#39;t shadow this when declaring size above I get variable-sized 
  27. // object may not be initialized
  28. j=(j/2);   // I don&#39;t actually know if this will result rounding down the .5 since its an 
  29. //int but that&#39;s really the least of my worries right now
  30. }          
  31. else
  32. {
  33. int array[h]=0;   //the point of the code is to print each of the chars in the message to an 8 digit binary
  34. j=(j/2);
  35. }
  36. for ( int y=0;y &lt; 8; y++)
  37. {
  38. printf(&quot;%i&quot;,array[y]);  //I understand that I am shadowing the array but if i change  the name of the array to array1 then its a new array and would get error variable sized object may not be initialized
  39. }
  40. printf(&quot;\n&quot;);
  41. i++;
  42. }
  43. }
  44. printf(&quot;\n&quot;);
  45. }
  46. &#39;)
  47. {
  48. int j= message[i]; // this converts char to ASCII
  49. for ( h=7;h&gt;=0;h--)
  50. {
  51. if (j % 2 == 1)
  52. {
  53. int array[h]=1;// if I don&#39;t shadow this when declaring size above I get variable-sized
  54. // object may not be initialized
  55. j=(j/2); // I don&#39;t actually know if this will result rounding down the .5 since its an
  56. //int but that&#39;s really the least of my worries right now
  57. }
  58. else
  59. {
  60. int array[h]=0; //the point of the code is to print each of the chars in the message to an 8 digit binary
  61. j=(j/2);
  62. }
  63. for ( int y=0;y &lt; 8; y++)
  64. {
  65. printf(&quot;%i&quot;,array[y]); //I understand that I am shadowing the array but if i change the name of the array to array1 then its a new array and would get error variable sized object may not be initialized
  66. }
  67. printf(&quot;\n&quot;);
  68. i++;
  69. }
  70. }
  71. printf(&quot;\n&quot;);
  72. }

so I'm sure there are plenty of problems with this code and that it could be better optimized but all I really care about is how I can declare the size of the array without shadowing local variable. Also I had to rewrite all this code line by line copying what I have in visual studio so there could be an syntax error somewhere here that's not in my actual code this is unfinished even with the array error fixed I will still need to print it as light emoji and not ones and zeros.

答案1

得分: 2

这是一个int类型的数组声明(大小为7

  1. int array[7];

这与上面的声明相同,但带有初始化

  1. int array[7] = {0, 1, 2, 3, 4, 5, 6};

进一步的代码可以通过标识符array访问该数组,如下所示

  1. array[h] = 42;

但如果你在上面的表达式前面加上关键字int(例如int array[h] = {42};),那是一个新的int类型数组声明(大小为h)。由于已经存在具有该名称的变量,新声明会“遮蔽”以前的声明。因此编译器会发出警告(以确保你知道自己在做什么)。

英文:

This is a declaration of an array of type int (of size 7)

  1. int array[7];

this is the same declaration as above, but with initialization

  1. int array[7] = {0, 1, 2, 3, 4, 5, 6};

Further code can access that array via the identifier array, like this

  1. array[h] = 42;

but if you prefix the above expression with the keyword int (e.g. int array[h] = {42};), that is a new declaration of an array of type int (of size h). Since there already exists a variable with that name, the new declaration 'shadows' the previous one. Therefore the warning from the compiler (to make sure, that you know what you're doing).

huangapple
  • 本文由 发表于 2023年2月8日 13:49:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381798.html
匿名

发表评论

匿名网友

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

确定