我的代码需要帮助,它给了我一个错误控制可能到达非空函数的结尾
I need help on my code, it gives me an error control may reach end of non-void function
我被困在 cs50 上的 pset2 中,老实说我觉得我错过了很多东西,我不知道我是否可以在无法真正理解一些基础知识的情况下继续学习课程。
我正在努力完成这项工作,然后我想我会停下来继续学习一些关于 c 的基础知识。
我需要这方面的帮助以及更多评论,我将不胜感激。
所以基本上这是我的代码
#define _XOPEN_SOURCE
#include <cs50.h>
#include <stdio.h>
#include <crypt.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
bool crack(string given_hash);
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage ./crack hash\n");
return 1;
}
if (!crack(argv[1]))
return 1;
}
bool crack(string given_hash)
{
string Alphabet = "abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ";
char key[6];
char salt[3];
salt[2] = '[=10=]';
for (int x = 0; x < 2; x++)
{
salt[x] = given_hash[x];
}
// single-letter keys.
for (int i = 0; i < 52; i++)
{
key[0] = Alphabet[i], key[1] = '[=10=]';
string new_hash = crypt(key,salt);
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
}
// for 2-letter keys.
for (int i = 0; i < 52; i++)
{
key[0] = Alphabet[i], key[2] = '[=10=]';
for (int j = 0; j < 52; j++)
{
key[1] = Alphabet[j];
}
string new_hash = crypt(key,salt);
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
}
// for 3-letter keys.
for (int i = 0; i < 52; i++)
{
key[0] = Alphabet[i], key[3] = '[=10=]';
for (int j = 0; j < 52; j++)
{
key[1] = Alphabet[j];
for (int k = 0; k < 52; k++)
{
key[2] = Alphabet[k];
}
}
string new_hash = crypt(key,salt);
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
}
// for 4-letter keys.
for (int i = 0; i < 52; i++)
{
key[0] = Alphabet[i], key[4] = '[=10=]';
for (int j = 0; j < 52; j++)
{
key[1] = Alphabet[j];
for (int k = 0; k < 52; k++)
{
key[2] = Alphabet[k];
for( int l = 0; l < 52; l++)
{
key[3] = Alphabet[l];
}
}
}
string new_hash = crypt(key,salt);
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
}
// for 5-letter keys.
for (int i = 0; i < 52; i++)
{
key[0] = Alphabet[i], key[5] = '[=10=]';
for (int j = 0; j < 52; j++)
{
key[1] = Alphabet[j];
for (int k = 0; k < 52; k++)
{
key[2] = Alphabet[k];
for(int l = 0; l < 52; l++)
{
key[3] = Alphabet[l];
for(int m = 0; m < 52; m++)
{
key[4] = Alphabet[m];
}
}
}
}
string new_hash = crypt(key,salt);
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
}
}
现在我不知道我做错了什么,我知道这个错误是由于在非 void 函数中没有返回值,但我该如何修复它?
您的 main
和 crack
函数都有一个代码路径,可能会到达函数的结尾,并且不会 return 一个值。
main
- 如果破解 returns 1
或 true
那么代码将在没有 returning int 值的情况下到达 main 的末尾。但是 main
有一个例外,如果您没有明确 return,那么它将 return 0
。所以虽然不是问题,但我仍然会确保所有代码路径 return.
crack
- 如果 none 的测试找到匹配的密码哈希,那么函数将到达终点而不是 return。根据功能逻辑,它永远不会发生,但编译器不知道。
要解决此问题,您需要确保所有代码路径 return 一个值。
您正在获取错误控制可能到达非空函数的结尾,因为 crack()
函数假设为 return bool
并且没有 return 语句crack()
函数在最后。编译代码时,编译器发现crack()
函数的所有return语句都可以达到只有如果满足某些条件
bool crack(string given_hash)
{
....
....
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
....
....
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
....
....
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
....
....
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
....
....
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
}
//<================ Return statement missing
}
如果满足none的条件,则控制有可能到达功能结束。您应该在 crack()
函数的末尾添加一个 return 语句,并且 return 一个指示失败的值。由于在所有情况下你都在 returning 0
(似乎是成功案例),你可以在最后 return 1
表示失败:
....
....
return 1;
}
注意main()
函数有一个例外,如果控制到达main()
函数的末尾而没有遇到return语句,则执行return 0;
。
我被困在 cs50 上的 pset2 中,老实说我觉得我错过了很多东西,我不知道我是否可以在无法真正理解一些基础知识的情况下继续学习课程。 我正在努力完成这项工作,然后我想我会停下来继续学习一些关于 c 的基础知识。 我需要这方面的帮助以及更多评论,我将不胜感激。
所以基本上这是我的代码
#define _XOPEN_SOURCE
#include <cs50.h>
#include <stdio.h>
#include <crypt.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
bool crack(string given_hash);
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage ./crack hash\n");
return 1;
}
if (!crack(argv[1]))
return 1;
}
bool crack(string given_hash)
{
string Alphabet = "abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ";
char key[6];
char salt[3];
salt[2] = '[=10=]';
for (int x = 0; x < 2; x++)
{
salt[x] = given_hash[x];
}
// single-letter keys.
for (int i = 0; i < 52; i++)
{
key[0] = Alphabet[i], key[1] = '[=10=]';
string new_hash = crypt(key,salt);
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
}
// for 2-letter keys.
for (int i = 0; i < 52; i++)
{
key[0] = Alphabet[i], key[2] = '[=10=]';
for (int j = 0; j < 52; j++)
{
key[1] = Alphabet[j];
}
string new_hash = crypt(key,salt);
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
}
// for 3-letter keys.
for (int i = 0; i < 52; i++)
{
key[0] = Alphabet[i], key[3] = '[=10=]';
for (int j = 0; j < 52; j++)
{
key[1] = Alphabet[j];
for (int k = 0; k < 52; k++)
{
key[2] = Alphabet[k];
}
}
string new_hash = crypt(key,salt);
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
}
// for 4-letter keys.
for (int i = 0; i < 52; i++)
{
key[0] = Alphabet[i], key[4] = '[=10=]';
for (int j = 0; j < 52; j++)
{
key[1] = Alphabet[j];
for (int k = 0; k < 52; k++)
{
key[2] = Alphabet[k];
for( int l = 0; l < 52; l++)
{
key[3] = Alphabet[l];
}
}
}
string new_hash = crypt(key,salt);
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
}
// for 5-letter keys.
for (int i = 0; i < 52; i++)
{
key[0] = Alphabet[i], key[5] = '[=10=]';
for (int j = 0; j < 52; j++)
{
key[1] = Alphabet[j];
for (int k = 0; k < 52; k++)
{
key[2] = Alphabet[k];
for(int l = 0; l < 52; l++)
{
key[3] = Alphabet[l];
for(int m = 0; m < 52; m++)
{
key[4] = Alphabet[m];
}
}
}
}
string new_hash = crypt(key,salt);
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
}
}
现在我不知道我做错了什么,我知道这个错误是由于在非 void 函数中没有返回值,但我该如何修复它?
您的 main
和 crack
函数都有一个代码路径,可能会到达函数的结尾,并且不会 return 一个值。
main
- 如果破解 returns 1
或 true
那么代码将在没有 returning int 值的情况下到达 main 的末尾。但是 main
有一个例外,如果您没有明确 return,那么它将 return 0
。所以虽然不是问题,但我仍然会确保所有代码路径 return.
crack
- 如果 none 的测试找到匹配的密码哈希,那么函数将到达终点而不是 return。根据功能逻辑,它永远不会发生,但编译器不知道。
要解决此问题,您需要确保所有代码路径 return 一个值。
您正在获取错误控制可能到达非空函数的结尾,因为 crack()
函数假设为 return bool
并且没有 return 语句crack()
函数在最后。编译代码时,编译器发现crack()
函数的所有return语句都可以达到只有如果满足某些条件
bool crack(string given_hash)
{
....
....
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
....
....
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
....
....
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
....
....
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
....
....
if (strcmp(new_hash, given_hash) == 0)
{
printf("you got the key: %s\n",key);
return 0;
}
}
//<================ Return statement missing
}
如果满足none的条件,则控制有可能到达功能结束。您应该在 crack()
函数的末尾添加一个 return 语句,并且 return 一个指示失败的值。由于在所有情况下你都在 returning 0
(似乎是成功案例),你可以在最后 return 1
表示失败:
....
....
return 1;
}
注意main()
函数有一个例外,如果控制到达main()
函数的末尾而没有遇到return语句,则执行return 0;
。