LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2015, 05:47 PM   #1
retroCheck
Member
 
Registered: Jan 2015
Posts: 34

Rep: Reputation: Disabled
Not able to return pointer from C function


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.

Code:
char * getRdir(char* arg) {
		char *dirptr = (char *) malloc(sizeof(2));
		char *cmpl = "<";
		char *cmpll = "<<";
		char *cmpr = ">";
		char *cmprr = ">>";
	if (strcmp(arg, cmpl) == 0) {
		dirptr = "<";
		printf("%s%c\n", "This is a dir - ", *dirptr);
	}else if(strcmp(arg,cmpll)){

	}
	return dirptr;
}
 
Old 02-13-2015, 05:55 PM   #2
retroCheck
Member
 
Registered: Jan 2015
Posts: 34

Original Poster
Rep: Reputation: Disabled
Durp, after struggling for this for awhile I come to find out that I had a void return from where I was declaring the function. It works fine now.
 
Old 02-13-2015, 05:59 PM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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
 
Old 02-13-2015, 06:04 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by retroCheck View Post
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.
 
Old 02-13-2015, 06:22 PM   #5
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
What's wrong with casting malloc's return value?

Also, strdup is non-standard so keep that in mind if you want your program to be super-portable.
 
Old 02-13-2015, 06:33 PM   #6
retroCheck
Member
 
Registered: Jan 2015
Posts: 34

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get return value of function mortonmorton Linux - Newbie 4 10-09-2009 09:54 PM
( C ) How do you declare a function pointer where a parameter is a function pointer? spursrule Programming 5 11-27-2007 07:56 PM
Return of the pointer rubadub Programming 6 08-15-2007 07:41 PM
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
why malloc() return different pointer value? hddd Programming 1 05-23-2005 03:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration