Hi all, have a C question that's probably going to be really simple to those in the know, but this is really frustrating me at the moment as I can't figure out how to solve it.
I'm in the middle of writing a program and one of the functions gets the path of a specified file, using realpath(). I'm using the first answer to this at the moment: http://stackoverflow.com/questions/1563168/example-of-realpath-function-c-programming
If I put that code in main(), it works fine. However, when I try to use it in my get_path() function, I end up with warnings when compiling. Here is the code I am using:
When compiling, I get the following output:
Could someone tell me what I'm doing wrong, what I should be doing and, more importantly, why I should be doing it? I've looked all over the place and I'm really not sure. I'm not very experienced with C and I'm learning as I go, so any help would really be appreciated. I want to understand where I'm going wrong here, because I think I have enough knowledge to write the rest of the program and figure out how to use libcurl (I'm writing a program to upload images to a popular hosting site), but I'm really stumbling with this.
Also, feel free to point and laugh and call me a noob since it's probably obvious!
I'm in the middle of writing a program and one of the functions gets the path of a specified file, using realpath(). I'm using the first answer to this at the moment: http://stackoverflow.com/questions/1563168/example-of-realpath-function-c-programming
If I put that code in main(), it works fine. However, when I try to use it in my get_path() function, I end up with warnings when compiling. Here is the code I am using:
Code:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
char get_path(char *filename);
int main(int argc, char **argv)
{
char filepath = get_path(argv[1]);
printf("%s", filepath);
return 0;
}
/* Get the full path of the file */
char get_path(char *filename)
{
char buf[PATH_MAX + 1];
char *res = realpath(filename, buf);
if (res) {
return buf;
} else {
perror(filename);
exit(EXIT_FAILURE);
}
}
When compiling, I get the following output:
Code:
gcc filepath.c -o filepath
filepath.c: In function ‘get_path’:
filepath.c:22:3: warning: return makes integer from pointer without a cast [enabled by default]
filepath.c:22:3: warning: function returns address of local variable [enabled by default]
Could someone tell me what I'm doing wrong, what I should be doing and, more importantly, why I should be doing it? I've looked all over the place and I'm really not sure. I'm not very experienced with C and I'm learning as I go, so any help would really be appreciated. I want to understand where I'm going wrong here, because I think I have enough knowledge to write the rest of the program and figure out how to use libcurl (I'm writing a program to upload images to a popular hosting site), but I'm really stumbling with this.
Also, feel free to point and laugh and call me a noob since it's probably obvious!
