ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
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
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.
static char buff[BUFFSIZE];
char *retstr=(char *)buff;
printf("Please enter a string:");
fgets(retstr, BUFFSIZE -1 , stdin);