473,796 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(&tsts tr);

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 1681
"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(&tsts tr);
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
2478
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 are sometime NULL. Using the desktop integration package we have which interfaces with MS Word when printing an address in a letter the end results often end up looking like this. 1 Any Street AnyTown
4
8411
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 now: <script language="JavaScript">
2
1782
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 local weather so I don't have any problems with that, but other has weather for several different locations. Basically I need to select only weather for location if I know its code. I should have exactly same dataset so I will be able bind it to the...
32
14903
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
1
1702
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 public properties of a custom Control neatly tied together -- rather than all over the IDE. One such set of values that will rarely be changed, so should have little priority in the IDE Properties panel, and therefore a good candidate for
3
1940
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 to attributes in tags (think Style tag -> Style object). The problem that I am chasing is: Html side:
9
2356
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 delimiting characters can be specified on the command line. The default delimiting characters built into the program are space, newline, tab, carriage return, form feed, vertical tab, comma and null. If a 'u' or 'U' is specified as the last command line...
1
3725
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 attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
0
10603
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 characters or simply matching a single character) : ANSI-89 - Mainly used only by Jet / ACE SQL ANSI-92 - Mainly used by SQL Server and other grown-up products In the later versions of Access it is now possible to select ANSI-92 compatibility as an...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.