Why does the following code, produce the following output?
UPDATING MY CODE TO THIS:
i get basically the same thing.
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
//==============================MAIN=======================================
#ifdef _WIN32
int main(int argc, char **argv)
{
char *path = (char*)malloc(sizeof(char)*FILENAME_MAX);
GetCurrentDir(path, sizeof(path));
printf("path: %s\n", path);
//other stuff
}
//==============================END========================================
OUTPUT
path : –
You’re returning a pointer to a local variable declared in
get_current_path, i.e., a variable that will likely be cleaned up once the function returns. You should accept a buffer as an argument and fill it for the caller, i.e.,PER YOUR EDIT:
sizeof(path)is going to be 4 or 8 (32-bit or 64-bit) as it is now just a pointer, not an array. You need to pass in the actual size, i.e.,sizeof(char) * FILENAME_MAX, so…