Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

IronFist

How can I return a string from a function?

This topic is 6156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I don''t even know if this is possible, but it would be very useful if it was. I am calling the function like this: char text[80]; text=textgetter(); Then in textgetter(), I want it to get some text from the keyboard, and return it back into my text variable:
int textgetter()
{
    char inputtext[80];

   //get text here

    return inputtext;
}
 
This obviously doesn''t work, but I don''t know how to make it work. Is there any way to make a function return a string instead of an int?

Share this post


Link to post
Share on other sites
Advertisement
void SomeFunction()
{
char text[80];

textgetter( text );

// Do something with text
}

void textgetter( char text[] )
{
// Fill text with something...
// Like strcpy( text, "the text" );
}

Alternatively, you may want to check out a C/C++ book...

G''luck,
-Nathan

Share this post


Link to post
Share on other sites
You could use the STL string class or pass a pointer to a string to the function like this.

char text[80];
textgetter(text);

void textgetter(char *string)
{
//get text here and place in string
}


Jack

Edited by - JackNathan on July 16, 2001 6:42:31 PM

Share this post


Link to post
Share on other sites
Hmm... Doesn't function(blaa); actually give a copy of blaa's value to the function. According to the book I have been reading (during the last few years ) you would need to use function(&blaa); so that the function would get the address of the original variable(blaa) instead of a copy of its value.. I think it should be like this :
textgetter(&text); so the pointer defined in the function would actually point to the original "text" string. Of course I'm probably wrong, but that's just the impression I have gotten...

(edit) Oops, I WAS wrong.. It seems though that both & and without worked, but & gave a warning.. Whats the deal with this '&' anyway?

Edited by - Afterlife on July 17, 2001 3:36:57 AM

Share this post


Link to post
Share on other sites
Alright, below follows the definitive answer, as opposed something that isn''t quit the definitive answer.

// this is your string
char text[80];

// this is you passing the address of the string to your function
// and accepting its return value for passing into another argument,
// in this case, printf ()

printf ("%s\n". textgetter (text));


// and this is the definition of the function

char *textgetter (char *text)
{
// do whatever you want with the pointer to the text array here
...
...

// return the address of text here
return text;
}

do yourself a favor, and return the address of text, to give yourself more flexibility in how you call your own function.



Share this post


Link to post
Share on other sites
quote:
Original post by Afterlife
Hmm... Doesn't function(blaa); actually give a copy of blaa's value to the function. According to the book I have been reading (during the last few years ) you would need to use function(&blaa); so that the function would get the address of the original variable(blaa) instead of a copy of its value.. I think it should be like this :
textgetter(&text); so the pointer defined in the function would actually point to the original "text" string. Of course I'm probably wrong, but that's just the impression I have gotten...

(edit) Oops, I WAS wrong.. It seems though that both & and without worked, but & gave a warning.. Whats the deal with this '&' anyway?


The deal with the &, is it evaluates to the address of what follows it. &text[0] evaluates to the address of text, which can simply be stated as text. &text evaluates to bullshit, and is not a good idea. &text[10] evaluates to the address of the 10th element of the array text.





Edited by - bishop_pass on July 17, 2001 3:51:18 AM

Share this post


Link to post
Share on other sites
&text does not evaluate to bullshit, it returns the address
of that variable.
so if text was declared as a pointer to type char, then &text
should return the address of that pointer, not bullshit

Share this post


Link to post
Share on other sites
quote:
Original post by Xtreme
&text does not evaluate to bullshit, it returns the address
of that variable.
so if text was declared as a pointer to type char, then &text
should return the address of that pointer, not bullshit


True, except in the context of the above discussion, its value is equiivalent to bullshit. The value text was never declared as a pointer to type char, rather it is implicitly defined. You see, there is an explicitly declared variable here which is an array of 80 bytes called text[80]. There is an implicit pointer derived from this variable and the semantics of C, which is the pointer text. To then take the address of this implicit value is in my opinion, very bad coding practice.

Tell me, where exactly is the value of text stored? We know where the value of text[80] is stored, because we can get the address of text[80] by using the semantics of C and use the expression text. But, where in the hell is the pointer text? The answer is a little vague, and thus the reason it evaluates to bullshit.

Now, on the other hand, if we actually declared a pointer of type char *, called it text, then certainly it is acceptable coding practice to take the address of text via &text, and use this however we wish.



Share this post


Link to post
Share on other sites
Guest Anonymous Poster
So does anyone want to give me the lowdown on pointers? I had a hard time understanding exactly how to use them when I learned about them my Junior year. I understand how they work, and that they point to the address of data, but I don''t know how to impliment them into my own code. Are there any good tutorials on pointers that you know of?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There are plenty of books that explain pointers a lot better than I ever could, so I encourage you to do some searching. But here''s a little cheat sheet of sorts that might help you to see how pointers behave in code:

int x;
Type: int

&x
Type: int*

int* xptr = &x;
Type of xptr: int*
Type of &x: int*

*xptr = 12;
Type of *xptr: int
Type of 12: const int;
Significance: Putting the asterisk before the name of the pointer will yield the object of the pointer--the thing it''s pointing to. Earlier, xptr was initialized to hold the address of x.

xptr = 345;
Type of xint: int*
Type of 345: const int
Significance: The address 345 is being stored in the pointer xptr. Normally, you never want to store a specific value in a pointer.

int blah[80];
Type: int*
Significance: It''s a pointer to the first element in the array (if it were a character array, it''s be the first character in the string). The array is a linear block of memory which, in this case, is 80 consecutive int variables.

&blah;
Type: int**.
Significance: It contains the adress of the pointer of the first element in the array. In other words, it''s a pointer to a pointer targetted at the first element in the array.

&blah[n]
&(blah[n])
Type: int*
Significance: It''s a pointer to character the character n steps from the beginning of the array. The "index" n should not be thought of as an index but, rather, as an offset from the array''s beginning (it''s base address).

"Hello, world"
L"Hello, world"
Type: const char* and const wchar_t*, respectively
Significance: In this case, it''s a pointer to the first character in the array [string]. You can return this kind of string from a function since it''s an object that can be found in the program''s executable image

I hope some of this helps.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!