473,405 Members | 2,334 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,405 software developers and data experts.

Problem About Returning String From A Function

HELLO EVERY-BODY....
i've a problem in c++ . i want to return a string from a fuction. how can i do it....
plz give me the syntex of the prototype of that function, body and calling of the function.
please post an example by which i can understand it...and can implement it into my project......
thanks in advance.......
Jan 12 '08 #1
7 1755
weaknessforcats
9,208 Expert Mod 8TB
You can:
a) use a string return type
b) use a string pointer argument
c) use a string reference argument if the string already exists in the calling function.
Jan 12 '08 #2
You can:
a) use a string return type
b) use a string pointer argument
c) use a string reference argument if the string already exists in the calling function.

plz resolve this code.........

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. char *a()
  4. {
  5.  char *name;
  6.  cin>>name;
  7.  return(name);
  8. }
  9. void main()
  10. {
  11. clrscr();
  12. char *a();
  13. char *name1;
  14. name1=a();
  15. cout<<name1;
  16. getch();
  17. }
Jan 13 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
You are not returning a string.

You are returning a pointer to a char.

Where is your string???
Jan 13 '08 #4
gpraghuram
1,275 Expert 1GB
plz resolve this code.........


#include<iostream.h>
#include<conio.h>
char *a()
{
char *name;
cin>>name;
return(name);
}
void main()
{
clrscr();
char *a();
char *name1;
name1=a();
cout<<name1;
getch();
}
Why u are not allocating memory to the variable name?

Raghuram
Jan 14 '08 #5
Why u are not allocating memory to the variable name?

Raghuram



then how can i input a name or other charector arry form a function...like this code..... plz help me.... i need it in my project........
Jan 14 '08 #6
gpraghuram
1,275 Expert 1GB
then how can i input a name or other charector arry form a function...like this code..... plz help me.... i need it in my project........

You cant return a character array from a function as the character array will be gone as the function returns.
Instead you shuld declare a pointer,allocate memory for it,get the inpyt and then return it.
You have written most of it and i am adding the missing links

Expand|Select|Wrap|Line Numbers
  1. char *a()
  2. {
  3. char *name=new char[100];//I am keeping it as 100.Plese chage it as per ur req.
  4. cin>>name;
  5. return(name);
  6. }
  7.  
Thanks
Raghuram
Jan 15 '08 #7
You cant return a character array from a function as the character array will be gone as the function returns.
Instead you shuld declare a pointer,allocate memory for it,get the inpyt and then return it.
You have written most of it and i am adding the missing links

Expand|Select|Wrap|Line Numbers
  1. char *a()
  2. {
  3. char *name=new char[100];//I am keeping it as 100.Plese chage it as per ur req.
  4. cin>>name;
  5. return(name);
  6. }
  7.  
Thanks
Raghuram





thanks..... my prob is solved......
Jan 15 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Lee Mundie | last post by:
Hi, wonder if anyone can help me with this - This was originally a simple search routine returning results back to itself! Trying to be smart I tried to combine a no. items per page so I could...
117
by: Peter Olcott | last post by:
www.halting-problem.com
5
by: Alfonso Morra | last post by:
Hi, What is the recomended way of returning an STL container (e.g. std::string, std::vector etc fom a function? Is it by simply returning a local variable? (I doubt it) std::string...
3
by: Carramba | last post by:
hi! the code is cinpiling with gcc -ansi -pedantic. so Iam back to my question Iam trying to make program were I enter string and serach char. and funktion prints out witch position char is...
2
by: prince -=nore=- | last post by:
I have a page, where I'm retrieving a file location from a database and outputting it's contents (HTML) onto a web page. On the aspx page, I have referred to the code behind it using the line...
8
by: andyjgw | last post by:
Hi all, hope someone can help here, I'm really stuck. Reading and googling like mad, to no avail so far... Basically, my problem *seems* to boil down to type conversion: Here's a sample web...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
18
by: jslowery | last post by:
I am not completely knowledgable about the status of lexical scoping in Python, but it was my understanding that this was added in a long time ago around python2.1-python2.2 I am using python2.4...
6
by: Rahul K | last post by:
Hi I am working on Visual Studio on Windows. I have a function which return the list of all the IP Addresses of a machine: vector<char *getAllLocalIPAddress() { char localHostName; struct...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.