C Question

Soldato
Joined
19 Dec 2009
Posts
2,669
Location
Lancashire
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:

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! :p
 
Thanks for the replies. Admittedly, my pointer knowledge is sketchy and I'm still trying to get my head round that properly (have had it explained to me, but it's not sticking - even the K&R book isn't helping there), but I see what you're saying about the other issues and it makes sense now they've been pointed out to me! >.<

Being able to see the changes you've made is quite helpful, too. In terms of what I'm doing with it, this is going to form part of a program that will upload images to a website. I basically want the user to be able to run program [filename] and have it upload, but my mess around doing it in Perl a while ago required the full path to the file, which is why I've been trying to use realpath here. I'm not sure whether or not there is a better way. I looked at using getcwd, but I can see plenty of problems that could arise if I did that. The only alternative I found so far was realpath. :/

When you say strict old-school C, are you referring to the ANSI standard?
 
Cheers for that. If what you say is the case about K&R, perhaps I'm not ready for it yet. I know the basics, have a grasp on basic to intermediate programming concepts etc. but more advanced features of C (pointers, the pre-processor etc.) are still fairly new to me. Are there any resources aimed more at people who are more in the beginner to intermediate range for C, but have experience of other languages? I'd say I'm fairly competent when it comes to Perl, but that's a different kettle of fish.

With regards to ANSI, I don't think so.
 
Hehe. It doesn't compile with the -ansi flag passed to GCC, though. :p

I always feel bad for just looking around and hacking stuff together because it doesn't feel like I'm learning any of the concepts behind programming, just how to do a certain thing in a language. :o

Currently looking through some of the Stanford videos on YouTube to see if I can pick up the theory stuff as well, seems to be okay so far but some of it is also going way over my head (e.g. how a floating point number is represented in memory). With any luck I'll pick up the important bits, though :)
 
Back
Top Bottom