469,950 Members | 1,846 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Help Returning string values within brackets.

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
Jul 22 '05 #1
3 1499
"Paul Kirby" <ad***@initcorp.co.uk> wrote...
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
Jul 22 '05 #2
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
Jul 22 '05 #3
JKop <NU**@NULL.NULL> wrote:
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.
}

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by David M Loraine | last post: by
2 posts views Thread by Arthur Dzhelali | last post: by
32 posts views Thread by tshad | last post: by
9 posts views Thread by santosh | last post: by
1 post views Thread by Rahul | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.