very simple pass by reference question

Discussion in 'C++' started by michael, May 2, 2007.

  1. michael

    michael Guest

    Hi All,

    How do I pass a reference to a pointer and update the pointer in the
    function? I have:

    void goGetString(char *str){
    string inString;

    cin >> inString;
    str = new char[inString.length()+1];
    strcpy(str, inString.c_str());
    cout << "String is " << str << endl;
    }

    int main(){
    char *someStr;

    goGetString(&*someStr);
    cout << "String was " << someStr << endl;
    }

    why is someStr not changed ?

    Thanks for your help

    Michael
    michael, May 2, 2007
    #1
    1. Advertising

  2. michael

    pmouse Guest

    1. send the pointer to the pointer to char (ptc), because it's the
    ptc's value that you want to change

    void goGetString(char **str)
    {
    ...
    *str = new char[ ... ];
    strcpy ( *str, ... );
    }


    to call:
    char* someStr;
    goGetString( &someStr );

    2. send the reference to the pointer to char (ptc)
    void goGetString(char *& str)
    {
    ...
    str = new char[...]
    strcpy( str, ... );
    }

    to call:
    char* someStr
    goGetString(someStr);


    ---

    Regards,

    PQ
    pmouse, May 2, 2007
    #2
    1. Advertising

  3. You also have an issue concerning memory-management in your code. See if you
    can see what that issue is.

    tet
    Tor Einar =?UTF-8?B?VMO4bm5lc3Nlbg==?=, May 2, 2007
    #3
  4. michael

    Lionel B Guest

    On Wed, 02 May 2007 12:33:26 +0800, michael wrote:

    > Hi All,
    >
    > How do I pass a reference to a pointer and update the pointer in the
    > function? I have:


    #include <iostream>
    #include <string>

    using namespace std;

    > void goGetString(char *str){


    void goGetString(char*& str){ // takes _reference_ to pointer

    > string inString;
    >
    > cin >> inString;
    > str = new char[inString.length()+1];
    > strcpy(str, inString.c_str());
    > cout << "String is " << str << endl;
    > }
    >
    > int main(){
    > char *someStr;
    >
    > goGetString(&*someStr); // ??? This is just someStr


    goGetString(someStr);

    > cout << "String was " << someStr << endl;


    delete [] someStr; // prevent memory leak
    > }
    >
    > why is someStr not changed ?


    It wasn't changed because you passed the pointer in to goGetString() by
    _value_.

    BTW, why not let goGetString take a string& as parameter, rather than
    using char* ?

    #include <iostream>
    #include <string>

    using namespace std;

    void goGetString(string& str)
    {
    cin >> str;
    cout << "String is " << str << endl;
    }

    int main()
    {
    string someStr;

    goGetString(someStr);
    cout << "String was " << someStr << endl;
    }

    Much simpler and less error-prone.

    --
    Lionel B
    Lionel B, May 2, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    470
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,615
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    417
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    512
    Dave Thompson
    Feb 14, 2005
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,268
Loading...

Share This Page