0

This is my indexes function for pset 6:

    char* indexes(const char* path)
    {
    char php_path[] = "index.php";
    char html_path[] = "index.html";

    char full_php_path[strlen(path)+strlen(php_path)+2+1]; 
    strcpy(full_php_path, path);
    strcat(full_php_path, php_path);

    char full_html_path[strlen(path)+strlen(html_path)+2+1];
    strcpy(full_html_path, path);
    strcat(full_html_path, html_path);

    if(access(full_php_path, F_OK) != -1){
        return full_php_path;
    } else if(access(full_html_path, F_OK)!= -1){
        return full_html_path;
    }
    return NULL;
}

It throws an error: address of stack memory associated with local variable 'full_php_path' returned when compiled...

0

You try to return a local variable, this means that your variable is stored on the stack, and when the code block ends, the memory that holds your variable is freed, so that the pointer that stores the string points to a place not valid, which results in an error. To avoid you can use malloc, it is also possible to work declaring your variable as static

You must log in to answer this question.

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