Help Returning string values within brackets.

P

Paul Kirby

Hello All

I am trying to create a function that returns a string like the following:

// defined in headerfile //
void test_func(char *retstr);
// -------------------- //

// c/cpp File //

#include "headerfile.h"

char * tststr;
test_func(&tststr);

printf("Test Function returned: %s\n",tststr);

// test function //
void test_func(char *retstr)
{
retstr = "test";
}
// ------------ //

None of these seem to return the correct string value.
I use to use this alot ages ago but cannot seem to remember now :(

So if anyone can help me sort this out please let me know :)

Thanks in advance
Paul Kirby
 
V

Victor Bazarov

Paul Kirby said:
I am trying to create a function that returns a string like the following:

// defined in headerfile //
void test_func(char *retstr);
// -------------------- //

// c/cpp File //

#include "headerfile.h"

I will assumen that the following is inside a function.
char * tststr;
test_func(&tststr);

Since you're trying to get an address of 'tststr', then how
do you expect it to work? The 'test_func' is declared to have
its first argument as "pointer to char". When you take the
address of 'tststr', you get "a pointer to a pointer to char".
Those are not compatible. You should probably define

void test_func(char** retstr); // note two asterisks
printf("Test Function returned: %s\n",tststr);

// test function //
void test_func(char *retstr)
{
retstr = "test";

Changing the value of the argument passed by value (and the
pointer 'retstr' here is passed by value) has no effect outside
the function.

Considering my suggestion above, you should do

void test_func(char const ** retstr)
{
*retstr = "test";
}

then it might work. Of course to be correct here, the pointer
whose address you pass in has to point to const char.
// ------------ //

None of these seem to return the correct string value.
I use to use this alot ages ago but cannot seem to remember now :(

Use 'std::string' and pass by reference. Drop pointers altogether.

void test_func(std::string& str)
{
str = "test";
}

int main()
{
std::string s;
test_func(s);
std::cout << "Test function returned " << s << std::endl;
So if anyone can help me sort this out please let me know :)

Get a good book.

V
 
J

JKop

You want a function that returns a string.

Question 1

Can the string bufffer have a constant length? If so, go to 1A, if not go to
1B.


1A:

char[20] GetName()
{
char name[20] = "James Philips";

return name;
}



1B:

char* GetName()
{
char* name = new char[LENGTH];

strcpy("James Philips",name); //Might be wrong order

return name;
}

int main()
{
char* blah = GetName();

//Work with it

delete [] blah; //calling function is responsible
}


Regardless of whether the string buffer may have a constant length or not,
you have the option of using std::string:

std::string GetName();


-JKop
 
O

Old Wolf

JKop said:
You want a function that returns a string.

char[20] GetName()

Syntax error

Even if you meant:
typedef char char_20[20];
char_20 GetName()
a compiler error is required because arrays cannot be passed by value.
{
char name[20] = "James Philips";

return name;

When used in a value context, 'name' gets converted to a pointer to
the first element of the array. Which will cause UB if the return value
is ever used because the array will no longer exist after the function
has returned.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top