我认为我的函数返回了 NULL,因为我将其初始化为 NULL。但是如果不这样做,编译时会出现错误。
这只是我在 test.c 文件中制作的原型,用于测试。所以当我让它正常工作后,我会将 lookup 函数复制回正确的文件中。
如果有帮助的话,这是 cs50 的 pset6 的一部分。
我正在使用所有正确的库,但当我尝试包含它们时,我的代码预览出现了问题!
这只是我在 test.c 文件中制作的原型,用于测试。所以当我让它正常工作后,我会将 lookup 函数复制回正确的文件中。
如果有帮助的话,这是 cs50 的 pset6 的一部分。
const char* lookup(const char* extension);
int main(void)
{
const char* type = "css";
const char* ending = lookup(type);
printf("the exstension: %s\nis of type = %s\n", type, ending);
}
const char* lookup(const char* extension)
{
char temp[strlen(extension)];
for (int i = 0; i < strlen(temp); i++)
{
if (isalpha(extension[i]))
temp[i] = tolower(extension[i]);
}
printf("temp = %s\n", temp);
char* filetype = NULL;
if (strcmp(temp, "html") == 0)
strcpy(filetype, "text/html");
else if(strcmp(temp, "css") == 0)
strcpy(filetype, "text/css");
else if(strcmp(temp, "js") == 0)
strcpy(filetype, "text/js");
else if(strcmp(temp, "jpg") == 0)
strcpy(filetype, "image/jpg");
else if(strcmp(temp, "ico" ) == 0)
strcpy(filetype, "image/x-icon");
else if(strcmp(temp, "gif") == 0)
strcpy(filetype, "image/gif");
else if(strcmp(temp, "png") == 0)
strcpy(filetype, "image/png");
else
return NULL;
return filetype;
}
我正在使用所有正确的库,但当我尝试包含它们时,我的代码预览出现了问题!
return
的数量,因为我认为有很多return
是不好的做法(如果我错了,请纠正我)。而且我不能将字符串赋值给一个变量,因为那样是错误的。我需要复制它。 - mrfr