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.
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.
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
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(),...
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
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.
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);
}
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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.