Problem About Returning String From A Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pankaj255143
    New Member
    • Oct 2007
    • 17

    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.......
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #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.

    Comment

    • pankaj255143
      New Member
      • Oct 2007
      • 17

      #3
      Originally posted by weaknessforcats
      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.........

      [code=cpp]
      #include<iostre am.h>
      #include<conio. h>
      char *a()
      {
      char *name;
      cin>>name;
      return(name);
      }
      void main()
      {
      clrscr();
      char *a();
      char *name1;
      name1=a();
      cout<<name1;
      getch();
      }[/code]
      Last edited by sicarie; Jan 14 '08, 01:54 AM. Reason: Code tags

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You are not returning a string.

        You are returning a pointer to a char.

        Where is your string???

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          Originally posted by pankaj255143
          plz resolve this code.........


          #include<iostre am.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

          Comment

          • pankaj255143
            New Member
            • Oct 2007
            • 17

            #6
            Originally posted by gpraghuram
            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........

            Comment

            • gpraghuram
              Recognized Expert Top Contributor
              • Mar 2007
              • 1275

              #7
              Originally posted by pankaj255143
              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,allocat e memory for it,get the inpyt and then return it.
              You have written most of it and i am adding the missing links

              [code=cpp]
              char *a()
              {
              char *name=new char[100];//I am keeping it as 100.Plese chage it as per ur req.
              cin>>name;
              return(name);
              }
              [/code]

              Thanks
              Raghuram

              Comment

              • pankaj255143
                New Member
                • Oct 2007
                • 17

                #8
                Originally posted by gpraghuram
                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,allocat e memory for it,get the inpyt and then return it.
                You have written most of it and i am adding the missing links

                [code=cpp]
                char *a()
                {
                char *name=new char[100];//I am keeping it as 100.Plese chage it as per ur req.
                cin>>name;
                return(name);
                }
                [/code]

                Thanks
                Raghuram





                thanks..... my prob is solved......

                Comment

                Working...