1

Got stuck with a couple of problems after debugging what I could. I placed -> before the code lines where the problems occur. The first -> relates to question 1 the other three relate to question 2. Many thanks,

1) I get this error: *error: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char '. Note: I actually also don't understand:

Why I have to use strcmp to compare a single char (instead of simply checking if req_target[j] == """)

Why I need to use & before req_target (I simply found this out by debugging, not by understanding).

How I should input that I am looking for the " character. Should I write """ or '"' or something else?

2) I get this error: incompatible pointer to integer conversion assigning to 'char' from 'char [2]'. I simply want to put the null terminator at location j in the array. How do I do this?

/**
 * Parses a request-line, storing its absolute-path at abs_path 
 * and its query string at query, both of which are assumed
 * to be at least of length LimitRequestLine + 1.
 */
bool parse(const char* line, char* abs_path, char* query)
{
    // initialize local variables
    char* method = NULL;
    char* req_target = NULL;
    char* version = NULL;
    int i = 0;
    int j = 0;
    int k = 0;
    int l = 0;

    // store method
    do 
    {
        method[i] = line[i];
        i++;
    }
    while(strcmp(&line[i], " ") != 0);

    // check if method is GET
    if(strcmp(method, "GET") != 0)
    {
        error(405);
        return false;
    }

    // store request-target and check if does not contain "
    do
    {
        req_target[j] = line[i];
->      if(strcmp(&req_target[j], '"') == 0)
        {
            error(400);
            return false;
        }       
        i++;
        j++;
    }
    while(strcmp(&line[i], " ") != 0);
->  req_target[j] = "\0";

    // check if request-target starts with /
    if(strcmp(&req_target[0], "/") == 0)
    {
        error(501);
        return false;
    }

    // store abs_path
    j = 0;
    while(strcmp(&req_target[j], "?") == 0 || strcmp(&req_target[j], "\0") == 0)
    {
        abs_path[j] = req_target[j];
        j++;
    }
->  abs_path[j] = "\0";

    // there is no query
    if(strcmp(&req_target[j], "\0") == 0)
    {
        query = "";
    }

    // there is a query
    if(strcmp(&req_target[j], "?") == 0)
    {
        j++;
        do
        {
            query[k] = req_target[j];
            j++;
            k++;
        }
        while(strcmp(&req_target[j], "\0") == 0);
    }

    // store http version
    i++;
    while(strcmp(&line[i], "\n") == 0)
    {
        version[l] = line[i];
        l++;
        i++;
    }
->  version[l] = "\0";

    // check http version
    if(strcmp(version, "HTTP/1.1") != 0)
    {
        error(505);
    }

    return true;
}
1

It's a very subtle difference, but a very important "rule of C" to learn and master.

Anything contained in single-quotes is a char. Anything contained in double-quotes is a char* (string).

This line if(strcmp(&req_target[j], '"') == 0) gives *error: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char '. message because of the single-quotes. The answer to your question

Why I have to use strcmp to compare a single char (instead of simply checking if req_target[j] == """)?

is: you don't! And since you will be comparing a single char, the correction notation will be '"' (single-quote double-quote single-quote).

This should clarify the second problem. Since you are dealing with a char, the notation is '\0'

2
  • Thanks so much! That works. It compiles now, but just so readers know it is full of bugs:). – CB Nuji Aug 18 '16 at 9:26
  • Wow ! That helped, thank you ! – WitoldW Jan 23 '17 at 3:42

You must log in to answer this question.

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