473,804 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

returning strings

how do we return strings or arrays from a function? i tried..

char[] some_func() //where i thought char[] would tell the compiler
that the return is a string
{

char str[100];
//something;
return str;
}
thanks in advance!

Dec 7 '06 #1
36 2292
Geo
MC felon wrote:
how do we return strings or arrays from a function? i tried..

char[] some_func() //where i thought char[] would tell the compiler
that the return is a string
{

char str[100];
//something;
return str;
}
thanks in advance!
You're writting C++ not C, do it like this

std::string some_func()
{
std::string str;
// something;
return str;
}

Dec 7 '06 #2
i work on TC++ (some old version). Please suggest something that works
in the same.

Dec 7 '06 #3
MC felon wrote:
how do we return strings or arrays from a function? i tried..

char[] some_func() //where i thought char[] would tell the compiler
that the return is a string
{

char str[100];
//something;
return str;
}
That won't work too well since by the time the function returns the
value being returned is dead.

Did you read the FAQ?
Dec 7 '06 #4

how does the function 'gets()' return the string?

Dec 7 '06 #5
MC felon wrote:
>
how does the function 'gets()' return the string?
It doesn't. You give it the memory, and the function fills it.

Dec 7 '06 #6

Rolf Magnus wrote:
MC felon wrote:

how does the function 'gets()' return the string?

It doesn't. You give it the memory, and the function fills it.
You give it the memory, and the function fills it, and the function
merrily continues filling memory past the end of the chunk you gave it
until the input is used up, and, unless your program has ABSOLUTE
control over the length of the input[*], there is absolutely nothing
you can do to prevent this risk.

To the OP: Don't use gets. If you are going to use C funcitons for
string input, use fgets.
[*] prompting the user to only type a certain number of characters is
by no means absolute control.

Gavin Deane

Dec 7 '06 #7

MC felon wrote:
i work on TC++ (some old version). Please suggest something that works
in the same.
Please quote enough of the message you are replying to to provide
context. On its own, your message is meaningless. To do that from
Google Groups, don't use the broken Reply link at the bottom of the
message. Instead, click the Show Options link at the top and use the
Reply link revealed there. Thanks.

To answer your question, did you #include<string >, the standard header
that defines the C++ string class (and puts it in namespace std)?

As to your compiler, if you can't get this

#include <string>
std::string some_func()
{
std::string str;
// something;
return str;
}

to compile, you haven't got a hope of learning or writing C++ with that
compiler. Fortunately, there are sevral up-to-date and free C++
compilers available for you to upgrade to. If you're on Windows,
Microsoft's VC++8 compiler is free for example, and there are other
options. If you're on a different platform there will, I am sure, be
other options too.

Gavin Deane

Dec 7 '06 #8
Geo

MC felon wrote:
i work on TC++ (some old version). Please suggest something that works
in the same.
Since I don't know what TC++ is, I don't know what works,. but if it
doesn't support the STL it isn't really C++ as I know it.

If you don't know how to work with C style strings, I suggest you read
a book, it's far to complicated to describe here.

Dec 7 '06 #9

ok. thanks for the tip.
here's what i want the function to do.

allow the user to input a string. stop receiving input when user
presses tab.

its like gets, only that the input gets stopped when '\t' is pressed
and not '\r'.

how do i do this?

Dec 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
4521
by: Matthias Kaeppler | last post by:
Hi, I was wondering why library implementors often make getter functions return strings by value (copies). For example, in boost::filesystem the leaf() function returns an std::string by value. So does Gnome::Vfs::FileInfo::get_name(). Isn't that unnecessary overhead? I could as well return by reference to const-string and avoid
4
4988
by: Anthony | last post by:
Hello, I am writing a function that populates an array of pointers to strings. Both the number of strings in the array, and the lengths of the strings, are dynamic; in particular, the number of strings won't be known until just before the function returns. The problem is that in the calling function, I need to know both the address of the array, and the number of strings. My first thought was to pass a pointer to the array into the...
12
2756
by: LongBow | last post by:
Hello all, From doing a google serach in the newsgroups I found out that a string can't be returned from a function, but using a char* I should be able to do it. I have spent most of the day trying to get this to work, but been unable to solve my mistake. What the function should return is a file name used for creating logs files. It will look something like JN122345.log
7
2320
by: wonderboy | last post by:
Hey guys, I have a simple question. Suppose we have the following functions:- //-----My code starts here char* f1(char* s) { char* temp="Hi"; return temp;
6
430
by: karthika.28 | last post by:
Hi, I am writing a function that needs to return an array of strings and I am having some trouble getting it right. Can you help me answer the following questions? 1. How does the function return the array? 2. How should the function be declared? 3. How is the return value captured by the calling program?
4
1664
by: Jon Skeet | last post by:
I've just noticed something rather odd and disturbing. The following code displays "True": using System; class Test { public static void Main(string args) { string x = new string ("".ToCharArray());
1
2086
by: Randy | last post by:
Hello, I have a web service in which I'm doing a query to an Access database and returning the resulting XML data when I do the return from the service... public string AOS_Data(string sql) { CDFDBSoapService.DBWebService dbws = new CDFDBSoapService.DBWebService(); Do the connection stuff here... return dbws.queryDatabase(sql); }
5
5486
by: shyam | last post by:
Hi All I have to write a function which basically takes in a string and returns an unknown number( at compile time) of strings i hav the following syntax in mind char *tokenize(char *) is it ok?
3
11780
by: DG is a god.... | last post by:
Dear All , This is my first post - please go easy...! I have a DLL written in C++ that has the following function exported from it -: char** ListHandles(int *processID); The processID parameter is used to find all associated open file
9
7912
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public method to add to that list,
0
9711
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
10343
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
10335
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,...
1
7633
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
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
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
4306
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
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.