const char* lookup(const char* path)
    const char* MIME = NULL;
    const char* first = strchr(path, '.')+2;
    const char* last = strrchr(first, ' ');
//(need to declare a type here) len = last - first;
//(need to declare a type here) buf = sizeof(len);
    strncpy(buf, first, len);
    buf[len] = '\0';

    if(strcasecmp(buf , "css") == 0)
        MIME = "text/css";
        return MIME;
//Code continues with each MIME type//

I have been writing and rewriting this code. I keep getting errors when I try to compile concerning the type definitions for len and buf. Is this really the problem or is my code completely wrong?

1 Answer 1


The question is a little confusing because of the comments. The compiler complains about type definitions for len and buf. And you say in the comments:

//(need to declare a type here) len = last - first;
//(need to declare a type here) buf = sizeof(len);

To clarify, a "type definition" is "declaring" that a variable name has a certain "type". Like so: const char* MIME = NULL; In C, the first time you use a variable, you need to tell the computer (the compiler) how you will use the variable. Will it be an int? A string (char *)? &tc.

There are some problems in the code, too. Let's say you send a request for hello.html. path will be something like /home/ubuntu/workspace/pset6/server/public/hello.html. The pointer first will point to .html + 2. Which is ml. Not, I think, what you want or intend. And the pointer last will be, well, NULL, since it doesn't find a space at all. Looks like you don't need buf or len at all. strchr will give you exactly what you are looking for, if you use it properly. Review man strchr. It is pretty straightforward.

You might have to work through some other problems with this function, but this should get you through the compile problems.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .