473,326 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 1660
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David M Loraine | last post by:
I am a sql novice and would appreciate any help with the following problem. In a table I have property addresses stored in 6 fields. Field6 always hold the Post Code. However, fields 4 and 5...
4
by: Ken | last post by:
Hello I am trying to change the color of a font in a text box. I have been reading about and trying various examples found it this group, but still can't get it right. Here is where I am...
2
by: Arthur Dzhelali | last post by:
I have a two xml files schema is identical. When I read file into dataset and then bind dataset to the form. These are weather files we are getting from weather service. One file has only...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
1
by: Sky Sigal | last post by:
(PS: Cross post from microsoft.pulic.dotnet.framework.aspnet.webcontrols) I've been looking lately for a way to keep the Properties panel for Controls 'clean'... My goal is to keep similar...
3
by: Sky Sigal | last post by:
I coming unglued... really need some help. 3 days chasing my tail all over MSDN's documentation ...and I'm getting nowhere. I have a problem with TypeConverters and storage of expandableobjects...
9
by: santosh | last post by:
Hello all, I've put together a small program to count the number of characters and 'words' in a text file. The minimum length of a word, (in terms of no. of characters), as well as word...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
0
NeoPa
by: NeoPa | last post by:
ANSI-89 v ANSI-92 Before we get into all the various types of pattern matching that can be used, there are two ANSI standards used for the main types of wildcard matching (matching zero or more...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.