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

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

declaring array size shadows a local variable

问题

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

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

int main(void)
{
    char* message = get_string("message:");
    int i=0;
    int array[7]; //这是导致声明遮蔽局部变量的原因,还尝试过int array[]={0,0,0,0,0,0,0,0};
              //如果没有这行,下面会出现可变大小对象无法初始化的错误
    while (message[i] != '
#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    char* message = get_string("message:");
    int i=0;
    int array[7]; //这是导致声明遮蔽局部变量的原因,还尝试过int array[]={0,0,0,0,0,0,0,0};
              //如果没有这行,下面会出现可变大小对象无法初始化的错误
    while (message[i] != '\0')
    {
        int j= message[i]; //这将字符转换为ASCII
        for (int h=7; h>=0; h--)
        {
            if (j % 2 == 1)
            {
                array[h]=1;//如果在上面声明大小时不遮蔽这个,会出现可变大小对象无法初始化的错误
                j=(j/2); //我实际上不知道这是否会四舍五入到0.5,因为它是一个整数,但这真的不是我现在担心的主要问题
            }
            else
            {
                array[h]=0; //代码的目的是将消息中的每个字符打印成8位二进制
                j=(j/2);
            }
            for (int y=0; y < 8; y++)
            {
                printf("%i",array[y]); //我明白我正在遮蔽数组,但如果我将数组的名称更改为array1,那么它就是一个新的数组,会出现可变大小对象无法初始化的错误
            }
            printf("\n");
        }
        i++;
    }
    printf("\n");
}
'
)
{ int j= message[i]; //这将字符转换为ASCII for (int h=7; h>=0; h--) { if (j % 2 == 1) { array[h]=1;//如果在上面声明大小时不遮蔽这个,会出现可变大小对象无法初始化的错误 j=(j/2); //我实际上不知道这是否会四舍五入到0.5,因为它是一个整数,但这真的不是我现在担心的主要问题 } else { array[h]=0; //代码的目的是将消息中的每个字符打印成8位二进制 j=(j/2); } for (int y=0; y < 8; y++) { printf("%i",array[y]); //我明白我正在遮蔽数组,但如果我将数组的名称更改为array1,那么它就是一个新的数组,会出现可变大小对象无法初始化的错误 } printf("\n"); } i++; } printf("\n"); }

所以我确定这段代码存在很多问题,它可以更好地优化,但我真正关心的是如何在不遮蔽局部变量的情况下声明数组的大小。另外,我不得不逐行重新编写所有这些代码,从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:

#include &lt;cs50.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

int main(void)
{
char* message = get_string(&quot;message:&quot;);
int i=0;
int array[7]; //this is what gives declaration shadows local variable also have tried int array[]={0,0,0,0,0,0,0,0};
          //without this line I get the variable-sized object may not be initialized down bellow
while (message[i] != &#39;
#include &lt;cs50.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
int main(void)
{
char* message = get_string(&quot;message:&quot;);
int i=0;
int array[7]; //this is what gives declaration shadows local variable also have tried int array[]={0,0,0,0,0,0,0,0};
//without this line I get the variable-sized object may not be initialized down bellow
while (message[i] != &#39;\0&#39;)
{
int j= message[i]; // this converts char to ASCII
for ( h=7;h&gt;=0;h--)  
{
if (j % 2 == 1)
{
int array[h]=1;// if I don&#39;t shadow this when declaring size above I get variable-sized 
// object may not be initialized
j=(j/2);   // I don&#39;t actually know if this will result rounding down the .5 since its an 
//int but that&#39;s really the least of my worries right now
}          
else
{
int array[h]=0;   //the point of the code is to print each of the chars in the message to an 8 digit binary
j=(j/2);
}
for ( int y=0;y &lt; 8; y++)
{
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
}
printf(&quot;\n&quot;);
i++;
}
}
printf(&quot;\n&quot;);
}
&#39;) { int j= message[i]; // this converts char to ASCII for ( h=7;h&gt;=0;h--) { if (j % 2 == 1) { int array[h]=1;// if I don&#39;t shadow this when declaring size above I get variable-sized // object may not be initialized j=(j/2); // I don&#39;t actually know if this will result rounding down the .5 since its an //int but that&#39;s really the least of my worries right now } else { int array[h]=0; //the point of the code is to print each of the chars in the message to an 8 digit binary j=(j/2); } for ( int y=0;y &lt; 8; y++) { 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 } printf(&quot;\n&quot;); i++; } } printf(&quot;\n&quot;); }

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

int array[7];

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

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

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

array[h] = 42;

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

英文:

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

int array[7];

this is the same declaration as above, but with initialization

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

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

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:

确定