C - Why is my function returning NULL?

Felix Rosén

I think my function is returning NULL since I initilize it to it. But I get compiling error if I dont.

This is just a prototype that I made in a test.c file to test it. So when I get it to work I will copy back the lookup function back into the correct file.

This is part of pset6 of cs50 if that helps anyone.

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

I'm using all the correct libraries, it screwed up my code preview when I tried to include them!

ouah
 char temp[strlen(extension)];

You don't reserve the space for the trailing null character and you never set it! For example, char temp[strlen(extension) + 1] = {0};.

Then:

char* filetype = NULL;

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

filetype pointed object must be allocated, for example using malloc, otherwise strcpy is copying with a null pointer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Powershell - why is my function returning null?

From Dev

Why is my QueryParam returning null

From Dev

Why is my array returning null?

From Dev

Why is this mode function returning null?

From Dev

Why is my stubbed method returning null?

From Dev

Why is my member id column returning null?

From Dev

Why is my ajax call returning null

From Dev

Why is my member id column returning null?

From Dev

Why is my BitmapFactory.decodeByteArray returning null?

From Dev

Why is my jQuery widget returning 'object is not a function'?

From Dev

Why isn't my function returning?

From Dev

Why is my javascript function not returning true?

From Dev

Why isn't my function returning the string?

From Dev

Why my function is returning empty string in python?

From Dev

Why is my function returning a Unit instead of Int?

From Dev

Why is my recursive JavaScript function not returning the string?

From Dev

Why is my function returning an array with undefined as items?

From Dev

Why is my program returning null,null,0 ,0?

From Dev

Why is my query of my SQLite database returning null?

From Dev

Why is my c# program returning 0?

From Dev

Why is my c# program returning 0?

From Dev

Why function is not returning negative value in C?

From Dev

Why is UserManager.Find() returning null if my UserName is email based?

From Dev

Why is my Add Button Returning a Null Reference Error

From Dev

Why is UserManager.Find() returning null if my UserName is email based?

From Dev

Why is my toString method returning null in an array of elements in java?

From Dev

Why is my csv file path always returning null?

From Dev

Why is my webGL context returning as null, but also showing that it was retrieved?

From Dev

Why are my getters returning null when I try to send to the server?

Related Related

  1. 1

    Powershell - why is my function returning null?

  2. 2

    Why is my QueryParam returning null

  3. 3

    Why is my array returning null?

  4. 4

    Why is this mode function returning null?

  5. 5

    Why is my stubbed method returning null?

  6. 6

    Why is my member id column returning null?

  7. 7

    Why is my ajax call returning null

  8. 8

    Why is my member id column returning null?

  9. 9

    Why is my BitmapFactory.decodeByteArray returning null?

  10. 10

    Why is my jQuery widget returning 'object is not a function'?

  11. 11

    Why isn't my function returning?

  12. 12

    Why is my javascript function not returning true?

  13. 13

    Why isn't my function returning the string?

  14. 14

    Why my function is returning empty string in python?

  15. 15

    Why is my function returning a Unit instead of Int?

  16. 16

    Why is my recursive JavaScript function not returning the string?

  17. 17

    Why is my function returning an array with undefined as items?

  18. 18

    Why is my program returning null,null,0 ,0?

  19. 19

    Why is my query of my SQLite database returning null?

  20. 20

    Why is my c# program returning 0?

  21. 21

    Why is my c# program returning 0?

  22. 22

    Why function is not returning negative value in C?

  23. 23

    Why is UserManager.Find() returning null if my UserName is email based?

  24. 24

    Why is my Add Button Returning a Null Reference Error

  25. 25

    Why is UserManager.Find() returning null if my UserName is email based?

  26. 26

    Why is my toString method returning null in an array of elements in java?

  27. 27

    Why is my csv file path always returning null?

  28. 28

    Why is my webGL context returning as null, but also showing that it was retrieved?

  29. 29

    Why are my getters returning null when I try to send to the server?

HotTag

Archive