[SOLVED] Not able to return pointer from C function
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have allocated the memory for given variable and still not able to return a pointer. Not sure what I am doing wrong here. Any help would be great, thanks.
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150
Rep:
You are correctly creating your ptr with maloc but then you assign the pointer to a constant string 'dirptr="<" you would be best dropping the malloc and use strdup to assign the ppointer and set the data, or one of the other string assignment functions.
also drop the sizeof out of the malloc, sizeof returns the storage size of a variable etc, so you should just use 'malloc(2)'
You would be best setting the variable dirptr to NULL before doing any assignments that way on return from the function you can test for a NULL ptr anf if true the assignment faiked and yiu can take appropriate steps
I have allocated the memory for given variable and still not able to return a pointer.
You don't really explain what "not able" means, are you getting a compile error, a segfault, or...?
Code:
char *dirptr = (char *) malloc(sizeof(2));
I think you meant 2, not sizeof(2). Though you might need to 3 for the nul terminator if you intend to store "<<" there.
Code:
dirptr = "<";
You're overwriting the pointer you got from malloc() here, so this is a memory leak. You probably want something like strcpy(dirptr, "<"); Or use strdup() like Keith Hedger suggests.
I see now that little bit of code although able to compile still has issues. Thanks for the advice. I did end up using strdup(not concerned with portability).
What I mean by "not able" was that I was getting a "no new declaration..." error. This was because of not having the right return value at the function declaration. Oh did not see that memory leak, thanks.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.