LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-29-2005, 10:54 AM   #1
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 14.1
Posts: 193

Rep: Reputation: 30
can a function return a string?


Hi

Would the function work if it is written like this:

#define BUFSIZE 256

int main(void)
{
char string[BUFSIZ], copyString;

strncpy(copyString, getString(string), 11);

.....
}

char getString(char string[])
{
printf("Enter a string: ");
fgets(string, BUFSIZE, stdin);
return *string;
}

Thanks
 
Old 01-29-2005, 11:46 AM   #2
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Maybee but why would you do this?



the string passed to the function is just a pointer to the real string data. so if its modifyed in the function and the main program uses it again some where else the data would have been changed so its like it was returned with the new information.
 
Old 01-29-2005, 11:53 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Two things fixed below in red.
Code:
#define BUFSIZE 256

int main(void)
{
char string[BUFSIZ], copyString[BUFSIZ];

strncpy(copyString, getString(string), 11);

.....
}

char getString(char string[])
{
printf("Enter a string: ");
fgets(string, BUFSIZE, stdin);
return string;  /*  removed: *   */
}

Last edited by Hko; 01-29-2005 at 11:54 AM.
 
Old 01-29-2005, 11:56 AM   #4
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 14.1
Posts: 193

Original Poster
Rep: Reputation: 30
i need the function readString() to read in the 10 digit number and pass it to the main() function,
so actually i don't want to "return *string", but to "return string" (not the pointer).

How can i go about this?

If i enter "return string", then the compiler says:
warning: return makes integer from pointer without a cast
 
Old 01-29-2005, 12:35 PM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
this would be a better way to write this


Code:
 
#define BUFSIZE 256

void getString(char *); 
char string[BUFFSIZE];

int main(void)
{
char copyString[BUFSIZ];

 getString(string);

strncpy(copyString, string, 11);

.....
}

void getString(char string[])  
{
printf("Enter a string: ");
fgets(string, BUFSIZE, stdin);
}
 
Old 01-29-2005, 12:42 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by hubabuba
so actually i don't want to "return *string", but to "return string" (not the pointer).

How can i go about this?
Sorry, but you're out of luck then. In C, strings are pointers...

Quote:
If i enter "return string", then the compiler says:
warning: return makes integer from pointer without a cast
True. I forgot to fix the return type of getString(). I should be like this to fix the error:
Code:
char *getString(char string[])
{
    /* .... */
}
 
Old 01-29-2005, 01:59 PM   #7
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Hey Hko mind my ignorance but why would you ever return a string pointer instead of just using the string that was passed ?
 
Old 01-29-2005, 02:33 PM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by exvor
Hey Hko mind my ignorance but why would you ever return a string pointer instead of just using the string that was passed ?
You're right when you say it's not needed at all. True.
But it seems hubabuba wants it. And a valid reason to do it could be just convenience. Some standard library functions do it: e.g: gets(), fgets(), strcpy(), strcat(),...

Last edited by Hko; 01-29-2005 at 02:35 PM.
 
Old 02-18-2005, 11:42 AM   #9
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
By the way my code above causes wierdness in the new string created because its 256 char long if you printed it out it would print garbage after the 11th char. A way to prevent this would be needed such as maybe initlizing the array to spaces or something.


Tested in dos not linux so maybe nix dont have that issue :P
 
Old 02-18-2005, 12:00 PM   #10
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You have to be careful with strncpy(). If you pay attention to the man page you'll find:
Quote:
The strncpy() function is similar, except that not more
than n bytes of src are copied. Thus, if there is no null
byte among the first n bytes of src, the result wil not be
null-terminated.
So generally, after a strncpy() call you'll have to specifically add the '\0' to the end of the string yourself.
 
Old 02-18-2005, 12:05 PM   #11
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Indeed you are the man.


strings still confuse me a bit with programming.

I think memory management/ location conuses me more tho.
 
Old 02-18-2005, 01:09 PM   #12
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Not sure if anyone cared but this is my fixed and revised code that works properly with this method and also you can change the length of the string and the copy length as well

tested on dos in emulated environment with Turbo c v. 2.0

Code:
/* Testing out some programming code to copy a string over */


#include<stdio.h>
#define BUFFSIZE 256
#define CPYLETH 20

void getstring(char *);


char string[BUFFSIZE];


int main(void)
{
	char copystring[BUFFSIZE];

	getstring(string);

	strncpy(copystring, string , CPYLETH);

	copystring[CPYLETH] = '\0';

	printf(copystring);

	return 0;
}
void getstring(char string[])
{
	printf("Please enter a string:");
	fgets(string, BUFFSIZE , stdin);
}

Last edited by exvor; 02-18-2005 at 01:10 PM.
 
Old 02-18-2005, 02:58 PM   #13
gr33ndata
Member
 
Registered: Aug 2003
Location: DMZ
Distribution: Ubuntu
Posts: 144

Rep: Reputation: 15
Another implementation

Code:
/* Function that returns a pointer */


#include<stdio.h>
#define BUFFSIZE 256
#define CPYLETH 20

void getstring(char *);


char string[BUFFSIZE];


int main(void)
{
	char copystring[BUFFSIZE];

	strncpy(copystring, getstring() , CPYLETH);

	copystring[CPYLETH] = '\0';

	printf(copystring);

	return 0;
}

char * getstring()
{
                static char buff[BUFFSIZE];
                char *retstr=(char *)buff;
	printf("Please enter a string:");
	fgets(retstr, BUFFSIZE -1 , stdin);

                return retstr;
}
Notice the static buffer (thats how inet_ntoa() do it)
 
Old 03-06-2005, 02:51 PM   #14
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
In the red shows possible unessisary code. Reason it is is because it wont matter. The last char gets replaced by \0 regardless if its actually in the buffer or not. Ither way tho i dont belive it would use more or less memory ither way.


Code:
{
                static char buff[BUFFSIZE];
                char *retstr=(char *)buff;
	printf("Please enter a string:");
	fgets(retstr, BUFFSIZE -1  , stdin);

                return retstr;
}

Last edited by exvor; 03-06-2005 at 02:55 PM.
 
  


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
can a C function return value to Shell Script variable yarnar Programming 17 06-02-2010 05:54 PM
not calling copy constructor on function return jhorvath Programming 7 09-22-2009 12:43 PM
using return in recursive function hubabuba Programming 9 10-10-2005 09:59 AM
String return question k1ll3r_x Programming 3 11-10-2004 02:46 PM
string as return value in c raven Programming 6 02-10-2002 06:09 AM

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

All times are GMT -5. The time now is 04:18 PM.

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