Thread: Ask about function parameters and pass a reference.

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    68

    Smile Ask about function parameters and pass a reference.

    Code:
    	CString m_n, m_e, n_str, e_str; 
    	int n_strlen, e_strlen;
    	UINT k_keyID;
    
                    m_Proc.GetInfo(FileName, n_str, e_str, n_strlen, e_strlen, k_keyID);
    	
                    m_n = n_str;
                    m_e = e_str;
    These above is somepart of my code. m_Proc is object and call function GetInfo which have 5 function parameters (FileName, n_str, e_str, n_strlen, e_strlen, k_keyID)


    These below is detail of GetInfo function

    Code:
    void CExtract::GetInfo(const CString& m_FileName, CString n_str, CString e_str, int n_strlen, int e_strlen, UINT keyID)
    	{
    n_str ="1234";
    e_str = "578756";
    n_strlen = 4;
    e_strlen = 6;
    keyID = 48756;
    }
    but when I see n_str value after exit GetInfo function. I see n_str = {""}
    e_str = {""}

    I expect n_str = {"1234"} , e_str = {"578756"}

    Do you know how to solve my problem?
    Thank you for your answer.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you want to modify a variable through an argument, you must use a pointer (<datatype>*) or a reference (<datatype>&). Otherwise, the data will only exist locally in your function.

    This will not work as intended, since Var only exists locally in the function:
    Code:
    void ChangeVariable(int Var)
    {
       Var = 12345;
    }
    
    int main()
    {
       int MyVar = 0;
       ChangeVariable(MyVar);
       cout << MyVar;
       return 0;
    }
    This on the otherhand will pass the referense of the variable, instead of just its value:
    Code:
    void ChangeVariable(int& Var)
    {
       Var = 12345;
    }
    
    int main()
    {
       int MyVar = 0;
       ChangeVariable(MyVar);
       cout << MyVar;
       return 0;
    }
    Or if you prefer to use pointers:
    Code:
    void ChangeVariable(int* Var)
    {
       *Var = 12345;
    }
    
    int main()
    {
       int MyVar = 0;
       ChangeVariable(&MyVar);
       cout << MyVar;
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  2. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM