returning pointers from functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khiladi
    New Member
    • Oct 2007
    • 2

    #1

    returning pointers from functions

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

    why this code doesn't works?
    [CODE=cpp]#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;
    }[/CODE]
    Last edited by Ganon11; Oct 30 '07, 01:23 PM. Reason: Please use the [CODE] tags provided.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Does it compile without errors and warnings?

    Comment

    • Meetee
      Recognized Expert Contributor
      • Dec 2006
      • 928

      #3
      Originally posted by khiladi
      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:
      [CODE=CPP]
      #include <sstream>

      int i = 5;
      string str;
      stringstream out;
      out << i;
      str = out.str();
      [/CODE]

      and then cast string to char * c and then return c.

      Regards

      Comment

      • khiladi
        New Member
        • Oct 2007
        • 2

        #4
        Originally posted by zodilla58
        In order to convert an int to string, you can use:
        [CODE=CPP]
        #include <sstream>

        int i = 5;
        string str;
        stringstream out;
        out << i;
        str = out.str();
        [/CODE]

        and then cast string to char * c and then return c.

        Regards

        hiii..
        tried but not successfull
        here is my code
        const char* MyClass::Conver tinttostring(in t 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 = Convertinttostr ing(i);
        char ch = *ch_ptr;

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by khiladi
          const char* MyClass::Conver tinttostring(in t 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.

          Comment

          Working...