Advertisement

How can I return a string from a function?

Started by July 16, 2001 04:54 PM
11 comments, last by IronFist 23 years, 7 months ago
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?
Play Sumasshu, which is a game I programmed. CLICK HERE
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
Advertisement
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
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
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
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.



_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
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
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
Advertisement
&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


Yes! There are kangaroos in Australia but I haven't seen them...yet
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.



_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
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?
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.


This topic is closed to new replies.

Advertisement