#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    17
    Rep Power
    0

    assigning pointer in function argument


    I have a class called Quantity and a list<Quantity*> called gWorkspace.

    I'd like to write a function which will take a pointer to a Quantity and a string as arguments. The function would then search the list and assign the pointer to point to the Quantity located using the string. My current implementation for the function is:

    bool findVar(Quantity* poutVar, string nameString)
    {
    // iterator for workspace
    list<Quantity*>::iterator wsiter;



    for (wsiter = gWorkspace.begin();
    wsiter != gWorkspace.end();
    ++wsiter)
    { // loop through workspace
    if ((*wsiter)->getName() == nameString)
    { // found a match by name
    poutVar = *wsiter; // point output to this quantity
    return true; // success
    }
    }

    return false;
    }

    and my code to call the function is:

    pfoundVar = new Quantity;
    resultVar = findVar(pfoundVar,name);
    ....

    When i run the function and provide a name which produces a match the function returns true and assigns the pointer in the function correctly (i.e. poutVar is a valid pointer just after poutVar = *wsiter) but when control passes back to the calling code the pointer is invalid and attempting to access pfoundVar generates a segfault. any ideas?? thanks
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Remember that in C/C++, arguments are passed by value. Whatever you do to the argument within the function, it does not change the variable that was passed to it. That is why when we want to change a variable's value outside the function, we need to pass a pointer to that variable and dereference it within the function.

    That is exactly what your program needs to do. As it now stands, you pass in a pointer to a Quantity. With that pointer, you can change the Quantity that it is pointing to. However, any changes you make to the pointer itself will only have an effect within the function itself and will not affect the pointer outside of the function.

    For what you want to do, you need to pass a pointer to a pointer to a Quantity. Regard:
    Code:
    bool findVar(Quantity** poutVar, string nameString)
    {
    ...
    *poutVar = *wsiter; // point output to this quantity
    ...
    Now you are able to change what the Quantity** is pointing to.

    Of course, that is assuming that your usage of wsiter is correct.

    BTW, I've worked with functions that required three or four levels of referencing (ie, 3 or 4 asterixes were needed).
    Last edited by dwise1_aol; March 31st, 2003 at 12:13 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    17
    Rep Power
    0
    thanks a lot. that was a very clear and helpful reply!

IMN logo majestic logo threadwatch logo seochat tools logo