LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   can a function return a string? (http://www.linuxquestions.org/questions/programming-9/can-a-function-return-a-string-283706/)

hubabuba 01-29-2005 11:54 AM

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

exvor 01-29-2005 12:46 PM

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.

Hko 01-29-2005 12:53 PM

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: *  */
}


hubabuba 01-29-2005 12:56 PM

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

exvor 01-29-2005 01:35 PM

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);
}


Hko 01-29-2005 01:42 PM

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[])
{
    /* .... */
}


exvor 01-29-2005 02:59 PM

Hey Hko mind my ignorance but why would you ever return a string pointer instead of just using the string that was passed ?

Hko 01-29-2005 03:33 PM

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(),...

exvor 02-18-2005 12:42 PM

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

itsme86 02-18-2005 01:00 PM

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.

exvor 02-18-2005 01:05 PM

Indeed you are the man.


strings still confuse me a bit with programming.

I think memory management/ location conuses me more tho.

exvor 02-18-2005 02:09 PM

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);
}


gr33ndata 02-18-2005 03:58 PM

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)

exvor 03-06-2005 03:51 PM

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;
}



All times are GMT -5. The time now is 06:42 AM.