0

its been a while i have been trying to get my index function right. I know I am close because there is only one error in check50.. The code looks fine to me. I can't figure out what is wrong with it.. Please help.. my code for indexes function is below...

char* indexes(const char* path)
{

    int n = strlen(path);
    char* path_html = "index.html";
    char* path_php = "index.php";
    char* copy_html = malloc(sizeof(BYTE) * (n + strlen(path_html)));
    char* copy_php = malloc (sizeof(BYTE) * (n + strlen(path_php)));

    for (int i = 0, n =strlen(path); i < n; i++)
    {
        copy_html[i] = path[i];
        copy_php[i] = path[i]; 
    }

    strcat(copy_php, path_php);
    strcat(copy_html, path_html);

    if (!access(copy_php, F_OK))
    {
        free(copy_html);
        return copy_php;
    }
    free(copy_php);

    if (!access(copy_html, F_OK))
    {
        return copy_html;
    } 
    free(copy_html);
    return NULL;
}

the error I get :(.. https://sandbox.cs50.net/checks/236727c2ec164277b91f994c7360b7e0

Thank you.

0

strlen does not consider the character end string '\0' so surely it is a problem of lack of enough to accommodate the entire length of the strings, my suggestion is to change malloc to have the required length:

char* copy_html = malloc(strlen(path) +  strlen(path_html) + 1);

And the same for the other pointer.

2
  • Thanks fand or your help :) I tried it worked.
    – Abhijeet
    Dec 8 '16 at 16:57
  • Excellent, good luck with the rest of the program
    – MARS
    Dec 8 '16 at 17:04

You must log in to answer this question.

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