繁体   English   中英

C - 为什么我的函数返回NULL?

[英]C - Why is my function returning NULL?

我认为我的函数返回NULL因为我将它初始化为它。 但如果我不这样做,我会得到编译错误。

这只是我在test.c文件中进行测试的原型。 因此,当我开始工作时,我会将lookup功能复制回正确的文件中。

这是pset6cs50一部分,如果这有助于任何人。

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;
}

我正在使用所有正确的库,当我尝试包含它时,它搞砸了我的代码预览!

 char temp[strlen(extension)];

您没有为尾随空字符保留空间,并且您从未设置它! 例如, char temp[strlen(extension) + 1] = {0};

然后:

char* filetype = NULL;

if (strcmp(temp,  "html") == 0)
    strcpy(filetype, "text/html"); 

必须分配filetype指向对象,例如使用malloc ,否则strcpy使用空指针进行复制。

您确定该extension仅包含扩展名. 我更喜欢使用_stricmpstrcmpi来比较不区分大小写。 为什么你strcpy filetype而不是赋值? 你只有没有malloc指针:

const char* lookup(const char* extension)
{
const char* filetype = NULL;

if (_stricmp(extension, "html") == 0)
    filetype = "text/html"; 
else if(_stricmp(extension, "css") == 0)
    filetype = "text/css";

else if(_stricmp(extension, "js") == 0)
    filetype = "text/js";

else if(_stricmp(extension, "jpg") == 0)
    filetype = "image/jpg";

else if(_stricmp(extension, "ico" ) == 0)
    filetype = "image/x-icon";

else if(_stricmp(extension, "gif") == 0)
    filetype = "image/gif";

else if(_stricmp(extension, "png") == 0)
    filetype = "image/png";

return filetype;
}

或更好:

const char* lookup(const char* extension)
{
  char * ext[] = { "html", "text/html", "css", "text/css", "js", "text/js", "jpg", "image/jpg", NULL };


  for ( int i = 0; ext[i]; i += 2 )
  {
    if ( !stricmp( extension, ext[i] ) )
      return ext[i+1];
  }
  return NULL;
}

小心这个:

char temp[strlen(extension)];

在C中,字符串以NULL结尾,因此您实际上不会为终止字符保留空间,因此您的临时字符串实际上可能在运行时看起来更长。

改为:

char temp[strlen(extension)+1];

后来:

temp[i] = '\0';

您必须为终止null分配空间并终止字符串:

char temp[strlen(extension)+1];

for (int i = 0; i < strlen(temp); i++)
{
    if (isalpha(extension[i]))
        temp[i] = tolower(extension[i]);
}
temp[i]= '\0';

另请注意,如果扩展名包含数字或任何其他非字母字符,则不会复制该字符。

你在临时房中没有额外的'\\ 0'额外空间; 你需要写类似的东西

char temp[strlen(extension) + 1];

并分配一些空间来存储文件类型; 可能是写作

char filetype[50]; // 50 should be enought for your case 

不过,我建议使用strcasecmp()(函数比较两个字符串,忽略字符的大小写)而不是strcmp()并删除看似无用的文件类型。 这可能是这样的:

#include <stdio.h>
#include <strings.h>

const char *lookup(const char *extension);

int main(void)
{
    const char *const type = "css";
    const char *ending = lookup(type);
    printf("the exstension: %s\nis of type = %s\n", type, ending);
}

const char *lookup(const char *extension)
{
    if (strcasecmp(extension, "html") == 0)
        return "text/html";

    else if (strcasecmp(extension, "css") == 0)
        return "text/css";

    else if (strcasecmp(extension, "js") == 0)
        return "text/js";

    else if (strcasecmp(extension, "jpg") == 0)
        return "image/jpg";

    else if (strcasecmp(extension, "ico" ) == 0)
        return "image/x-icon";

    else if (strcasecmp(extension, "gif") == 0)
        return "image/gif";

    else if (strcasecmp(extension, "png") == 0)
        return "image/png";

    return NULL;
}

更具伸缩性的解决方案可以使用数组来描述扩展,因此如果添加新类型,则无需更改代码:

#include <stdio.h>
#include <strings.h>

struct Type {
    const char *const extension;
    const char *const mime;
} knownTypes[] = {
    { "html", "text/html"    },
    { "css",  "text/css"     },
    { "js",   "text/js"      },
    { "jpg",  "image/jpg"    },
    { "ico",  "image/x-icon" },
    { "gif",  "image/gif"    },
    { "png",  "image/png"    }
};

static const size_t nbKnownTypes = sizeof(knownTypes) / sizeof(struct Type);

const char* lookup(const char* extension);

int main(void)
{
    const char *const type = "Css";
    const char *ending = lookup(type);
    printf("the exstension: %s\nis of type = %s\n", type, ending);
}

const char *lookup(const char *extension)
{
    for (size_t i = 0; i < nbKnownTypes; i++) {
         struct Type type = knownTypes[i];
         if (strcasecmp(extension, type.extension) == 0)
             return type.mime;
    }

    return "Unknown mime type";
}

对于这种设置,您可以轻松添加新类型的扩展名和mime类型(您可以将此结构放在单独的c文件中,这可能会阻止重新编译所有内容,但这是另一个故事)

1.您的代码表现出未定义的行为。 在你的函数lookup -

char temp[strlen(extension)];     // basically char temp[3]

并使用循环填充完整数组,不留空间'\\0'然后使用%s打印并将其传递给strcmp也会导致UB

像这样声明你的数组temp -

char temp[strlen(extension)+1)]={'\0'};        // +1 for null character

2.当您复制指针filetype -

if (strcmp(temp,  "html") == 0)
    strcpy(filetype, "text/html"); 

但它指向NULL因为它没有分配任何内存。

使用malloc将内存分配给filetype

char* filetype = NULL没有使用strcpy函数复制字符串的内存空间。 将此代码替换为char* filetype = malloc(20)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM