473,386 Members | 1,621 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,386 software developers and data experts.

returning pointers from functions

hiii
can you please tell how to return pointers from functions?

why this code doesn't works?
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. char* fn_name(int i)
  3. {
  4.         char c = itoa(i);
  5.         return &c;
  6. }
  7.  
  8. int main()
  9. {
  10.        int i = 10;
  11.        char* ch_ptr;
  12.        ch_ptr = fn_name(i);
  13.        cout<<&ch_ptr<<endl;
  14. }
Oct 30 '07 #1
4 1409
Banfa
9,065 Expert Mod 8TB
Does it compile without errors and warnings?
Oct 30 '07 #2
Meetee
931 Expert Mod 512MB
hiii
can you please tell how to return pointers from functions?

why this code doesn't works?
#include <iostream.h>
char* fn_name(int i)
{
char c = itoa(i);
return &c;
}

int main()
{
int i = 10;
char* ch_ptr;
ch_ptr = fn_name(i);
cout<<&ch_ptr<<endl;
}
In order to convert an int to string, you can use:
Expand|Select|Wrap|Line Numbers
  1. #include <sstream>
  2.  
  3. int i = 5;
  4. string str;
  5. stringstream out;
  6. out << i;
  7. str = out.str();
  8.  
and then cast string to char * c and then return c.

Regards
Oct 30 '07 #3
In order to convert an int to string, you can use:
Expand|Select|Wrap|Line Numbers
  1. #include <sstream>
  2.  
  3. int i = 5;
  4. string str;
  5. stringstream out;
  6. out << i;
  7. str = out.str();
  8.  
and then cast string to char * c and then return c.

Regards

hiii..
tried but not successfull
here is my code
const char* MyClass::Convertinttostring(int i)
{
string str;
stringstream out;
out << i;
str = out.str();
const char* ch = str.c_str();
return ch;
}

and then i use it as-
int i = 65;
const char* ch_ptr = Convertinttostring(i);
char ch = *ch_ptr;
Oct 30 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
const char* MyClass::Convertinttostring(int i)
{
string str;
stringstream out;
out << i;
str = out.str();
const char* ch = str.c_str();
return ch;
}
Everyone seems to ignore the fact that you are returning the address of a local variable. str and ch are destroyed when the function completes.

You can never return the address of a local variable.

You cannot return a pointer from a function unless you are sure it will continue to point to an undeleted value when the function returns. Usually, this means you will create your strings on the heap. But when you do that, you are responsible for deleting them, which brings in a bunch of other issues.
Oct 30 '07 #5

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

Similar topics

5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
7
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
by: Generic Usenet Account | last post by:
Is it okay to return a local datastructure (something of type struct) from a function, as long as it does not have any pointer fields? I think it is a bad idea, but one of my colleagues does not...
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
5
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 *) ...
26
by: cdg | last post by:
Could anyone correct any mistakes in this example program. I am just trying to return an array back to "main" to be printed out. And I am not sure how a "pointer to an array" is returned to the...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
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,...

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.