0

Here is my code for indexes in server.c of web server :

char* indexes(const char* path)
{
     if (access(path, R_OK | X_OK) == -1)
    {
        error(403);
        return NULL;
    }

    if (php == NULL)
    {
        return NULL;
    }

    char* php = "index.php";
    char html = malloc(12);
    if (html == NULL)
    {
        return NULL;
    }
    html = "index.html";
    char path1 = malloc(sizeof(path) + 11);
    if (path1 == NULL)
    {
        return NULL;
    }
    strcat(path1,php);
    char* path2 = malloc(sizeof(path) + 12);
    if (path2 == NULL)
    {
        return NULL;
    }
    path2 =  strcat((char*)path, html);
    // check if index.html or index.php exists and returns else returns NULL
    if (access(path1, R_OK | X_OK) == -1)
    {
        free (php);
        free (html);
        free (path2);
        return path1;
    }
    else if (access(path2, R_OK | X_OK) == -1)
    {
        free (php);
        free (html);
        free (path1);
        return (path2);
    }
    else
    {
        free(php);
        free(html);
        free(path1);
        free(path2);
        return NULL;

    }


}

and some of the errors when I try to compile using "make" are:

server.c:453:9: error: use of undeclared identifier 'php'
    if (php == NULL)
        ^
server.c:459:10: error: incompatible pointer to integer conversion initializing 'char' with an expression of type 'void *'

    char html = malloc(12);

         ^     
server.c:460:14: error: comparison between pointer and integer ('int' and 'void *') [-Werror]
    if (html == NULL)
         ^  
server.c:464:10: error: incompatible pointer to integer conversion assigning to 'char' from 'char [11]' [-Werror,-Wint-conversion]
    html = "index.html";'

         ^ 
server.c:465:10: error: incompatible pointer to integer conversion initializing 'char' with an expression of type 'void *'
      [-Werror,-Wint-conversion]
    char path1 = malloc(sizeof(path) + 11);

         ^       
server.c:466:15: error: comparison between pointer and integer ('int' and 'void *') [-Werror]
    if (path1 == NULL)

        ~~~~~ ^  ~~~~
server.c:470:12: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'char *'; take the address with &
      [-Werror,-Wint-conversion]
    strcat(path1,php);

           ^~~~~
           &
=
1
  • Because you have not formatted your code properly, it is many times more difficult for others to find your errors than it is for you. You can fix the formatting by; editing your post; select your code; click the {} in the icon bar atop the edit pane. Then repeat the process for the error block. – DinoCoderSaurus Apr 3 '16 at 16:13
0

See answer 2 in this post for a good and detailed explanation of the first error.

Next:

    server.c:459:10: error: incompatible pointer to integer conversion initializing 'char'   
with an expression of type 'void *'

        char html = malloc(12);

On the one hand (left side) you are declaring html as a char. A char is one-byte, full stop. On the other hand (right side) you are trying to create a pointer to 12 bytes of memory. Only one thing can be true. Either html is a 1-byte char OR html is a pointer (char*?) to 12 bytes of memory [Review the man page for malloc, specifically RETURN VALUE]. That cascades to the error at 460 (obviously our friend the compiler decided you meant html to be a char, so you cannot compare it to NULL, which is a pointer). Then flows down to the error at 464 (same thing, compiler decided char, programmer said string literal).

Poor path1 suffers the same fate as html.

You are on the the right track starting here char* php = "index.php";. With patience, attention to detail, and increased ability to understand compiler errors, you will achieve a clean compile.

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