在循环中找不到先前声明的变量

Previously declared variable not found in loop

我正在将 sha1 破解器的实施作为一个大学项目。 我正在为它们的哈希值尝试不同的值和循环。

for(int a=0; a<26; a++)
{
    for(int b=0; b<26; b++)
    {
        for(int c=0; c<26; c++)
        {
            for(int d=0; d<26; d++)
            {
                for(int e=0; e<26; e++)
                {

                    for(int f=0; f<26; f++)
                    {
                        result[0]=alphabet[a];
                        result[1]=alphabet[b];
                        result[2]=alphabet[c];
                        result[3]=alphabet[d];
                        result[4]=alphabet[e];
                        result[5]=alphabet[f];


                        //printf("result : %s\n candidate %x %x %x %x %x",result,candidate.a,candidate.b,candidate.c,candidate.d,candidate.e);

                        // Declarations
                        uint32_t a,b,c,d,e,temp;
                        uint32_t w[80]= {0};
                        a=h0;
                        b=h1;
                        c=h2;
                        d=h3;
                        e=h4;

下面的代码只是正在进行的 SHA-1 代码。 构建外部循环是为了测试每个选项的 6 个字符的单词。

如果我现在尝试将 uint32_t 声明行移动到第一个 for 循环之前(但仍在同一个函数中,C 编译器会警告我变量未使用并且程序崩溃,因为这些变量似乎在循环中丢失。但出于性能原因,我不想每次都声明它们是新的。 如果我尝试全局声明它们,则会发生同样的问题。但是当它们在最后一个循环中声明时,一切正常

您声明了两次变量。每个 for(int x 声明变量。您应该能够将 uint32_t a,b,c ... 移出循环并在第一个 for 之前,并且您需要从每个 for 语句中删除 int

这应该可以编译(我不知道代码是否可以工作或是否可以执行您希望它执行的操作,因为您似乎正在重置最内层循环内的循环变量):

uint32_t a,b,c,d,e,temp;
uint32_t w[80]= {0};

for(a=0; a<26; a++)
{
    for(b=0; b<26; b++)
    {
        for(c=0; c<26; c++)
        {
            for(d=0; d<26; d++)
            {
                for(e=0; e<26; e++)
                {

                    for(f=0; f<26; f++)
                    {
                        result[0]=alphabet[a];
                        result[1]=alphabet[b];
                        result[2]=alphabet[c];
                        result[3]=alphabet[d];
                        result[4]=alphabet[e];
                        result[5]=alphabet[f];


                        //printf("result : %s\n candidate %x %x %x %x %x",result,candidate.a,candidate.b,candidate.c,candidate.d,candidate.e);

                        // Declarations
                        a=h0;
                        b=h1;
                        c=h2;
                        d=h3;
                        e=h4;

或者如您在评论中所说,您需要重命名变量而不是重新定义循环变量,如下所示:

for(int a=0; a<26; a++)
{
    for(int b=0; b<26; b++)
    {
        for(int c=0; c<26; c++)
        {
            for(int d=0; d<26; d++)
            {
                for(int e=0; e<26; e++)
                {

                    for(int f=0; f<26; f++)
                    {
                        result[0]=alphabet[a];
                        result[1]=alphabet[b];
                        result[2]=alphabet[c];
                        result[3]=alphabet[d];
                        result[4]=alphabet[e];
                        result[5]=alphabet[f];


                        //printf("result : %s\n candidate %x %x %x %x %x",result,candidate.a,candidate.b,candidate.c,candidate.d,candidate.e);

                        uint32_t A,B,C,D,E,temp; // var "a" is not "A"
                        uint32_t w[80]= {0};


                        // Declarations
                        A=h0;
                        B=h1;
                        C=h2;
                        D=h3;
                        E=h4;

If i now try to move the uint32_t Declaration line before the first for loop ( but still within the same function, the C compiler warns me the variables are unused and the program crashes as those variables seem to be lost within the loop.

如果你把uint32_t一个; uint32_t b; ... uint32_t e;外,然后重新声明为 int a;诠释乙; ... 诠释 e; ,声明为 uint32_t 类型的变量超出范围。在最内层的 for 循环中,变量 a、b、...、e 是在 for 语句中声明的变量。毕竟 for 循环, int a, ..., e 超出范围,并且将使用 uint32_t 变量。但是他们没有被分配到想要的值。

如果将 uint32_t 声明放在内部 for 循环中,则 uint32_t 在范围内,而那些 int 在范围外(或者我应该说它们是 "overridden"?).

But for performance reasons, i do not want to declare them everytime new.

如果你真的想像现在这样保留嵌套循环,让编译器优化它。每次执行内部循环时都不会重新声明变量。您可以比较结果汇编代码。否则,请考虑@JonathanLeffler 的建议,将它们分成函数以使您的代码更具可读性。

如果我有任何错误,请不要犹豫指出it/them!谢谢!