pass by reference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Prabu Ramasamy
    New Member
    • Jun 2007
    • 6

    pass by reference

    how to pass char* value by creating new?

    here function definition is

    ConvertNumberTo String(double source, char *targetString)
    {

    }

    wil u pleas tell me how to pass (char* targetstring) value in main() bymeans of creating memory by 'new' method?
    Also explain Different types pass reference methods in c++?
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Originally posted by Prabu Ramasamy
    how to pass char* value by creating new?

    here function definition is

    ConvertNumberTo String(double source, char *targetString)
    {

    }

    wil u pleas tell me how to pass (char* targetstring) value in main() bymeans of creating memory by 'new' method?
    Also explain Different types pass reference methods in c++?
    well you know the type you want to return, char *, so put that in front of the name of the function.

    To allocate a char array, you use new char [dim_size] in C++ and malloc(sizeof(c har)*dim_size) in C. Note that the sizeof(char) is a little redundent, but is used exemplify how you would allocate for any array. Also note that in C, you do not have to cast to a char* when assigning it to a char*. malloc() returns a void * and a void * in C can be assigned to any pointer type and vice-versa.

    Enjoy.


    Adrian

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by AdrianH
      Quote:
      Originally Posted by Prabu Ramasamy
      how to pass char* value by creating new?

      here function definition is

      ConvertNumberTo String(double source, char *targetString)
      {

      }

      wil u pleas tell me how to pass (char* targetstring) value in main() bymeans of creating memory by 'new' method?
      Also explain Different types pass reference methods in c++?

      well you know the type you want to return, char *, so put that in front of the name of the function.
      You can also have the calling function pass in the address of the targetString pointer.

      [code=cpp]
      void ConvertNumberTo String(double source, char **targetString)
      {

      }
      [/code]

      Then, inside ConvertNumberTo String you:

      [code=cpp]
      *targetString = new char[...some size...];
      [/code]

      and that changes the address inside the pointer used by the calling function.
      [code=cpp]
      char* ptr = 0;
      ConvertNumberTo String(2.5, &ptr);
      cout << ptr; //you see your string.
      [/code]
      Last edited by AdrianH; Jun 9 '07, 07:23 PM. Reason: Added return type to function

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Also, instead of using a pointer to a pointer, you can use a reference to a pointer. i.e char *& targetString instead of char ** targetString.

        Of course, using cstrings in C++ is not necessary as wfc usually points out. You are better off using the string class.

        Adrian

        Comment

        Working...