Passing by reference vs. Value
-
Is this correct? What i am attempting to ensure is that the arrErrCode[i]var is being passed by reference to the TestMyStuff method. The question is by using a LPTSTR vs. an &, i should get the same result correct? Main { For (int 1= 0; i<2; i++) { arrErrCode[i] = new TCHAR[16]; ZeroMemory(arrErrCode[i], 16 * sizeof(TCHAR)) if (TestmyStuff(arrErrCode[i])) //do something here with arrErrCode[i] } } BOOL TestMystuff(LPTSTR szCode) { StrCpy(szCode, TEXT("000")); return TRUE; }
-
Is this correct? What i am attempting to ensure is that the arrErrCode[i]var is being passed by reference to the TestMyStuff method. The question is by using a LPTSTR vs. an &, i should get the same result correct? Main { For (int 1= 0; i<2; i++) { arrErrCode[i] = new TCHAR[16]; ZeroMemory(arrErrCode[i], 16 * sizeof(TCHAR)) if (TestmyStuff(arrErrCode[i])) //do something here with arrErrCode[i] } } BOOL TestMystuff(LPTSTR szCode) { StrCpy(szCode, TEXT("000")); return TRUE; }
-
Is this correct? What i am attempting to ensure is that the arrErrCode[i]var is being passed by reference to the TestMyStuff method. The question is by using a LPTSTR vs. an &, i should get the same result correct? Main { For (int 1= 0; i<2; i++) { arrErrCode[i] = new TCHAR[16]; ZeroMemory(arrErrCode[i], 16 * sizeof(TCHAR)) if (TestmyStuff(arrErrCode[i])) //do something here with arrErrCode[i] } } BOOL TestMystuff(LPTSTR szCode) { StrCpy(szCode, TEXT("000")); return TRUE; }
Of course
arrErrCode[i]
var is passed by value, anyway, since it is a pointer then things would go on smootlhy (provided you fix all the bugs: your code is a bunch of typos). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
So if i wanted to do it by reference then i would add an & to the function definition like : TestMyStuff(LPTSTR& szCode) { } I have never worked with LPTSTR's so i am just a tad confued with how to pass by reference using LPTSTR
You don't need it.
LPTSTR
is just a reassuring name given to a pointer type. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
So if i wanted to do it by reference then i would add an & to the function definition like : TestMyStuff(LPTSTR& szCode) { } I have never worked with LPTSTR's so i am just a tad confued with how to pass by reference using LPTSTR
A LPTSTR is a pointer to a char array. So you don't need to pass it by reference unless you want to change it size (in fact, allocating a new array). If you only wants to modify the contents, then you can pass the pointer.
Cédric Moonen Software developer
Charting control [v1.3] -
So if i wanted to do it by reference then i would add an & to the function definition like : TestMyStuff(LPTSTR& szCode) { } I have never worked with LPTSTR's so i am just a tad confued with how to pass by reference using LPTSTR
LCI wrote:
So if i wanted to do it by reference then i would add an &
Yes but the only reason to pass a pointer by reference is to change the value of the pointer (the address). Is that what you need to do? I would not guess that from the code you posted.
led mike
-
Is this correct? What i am attempting to ensure is that the arrErrCode[i]var is being passed by reference to the TestMyStuff method. The question is by using a LPTSTR vs. an &, i should get the same result correct? Main { For (int 1= 0; i<2; i++) { arrErrCode[i] = new TCHAR[16]; ZeroMemory(arrErrCode[i], 16 * sizeof(TCHAR)) if (TestmyStuff(arrErrCode[i])) //do something here with arrErrCode[i] } } BOOL TestMystuff(LPTSTR szCode) { StrCpy(szCode, TEXT("000")); return TRUE; }
Hi There is much confusion, in particular amongst novices, about Pointers, References snd Values when used as arguments. Using a Value is clear, a copy of the value gets placed on the stack. You can do what you want with it, it will be lost when the stack is cut down when you return from the procedure. A pointer is also clear, You're passing on the address of some data. That data resides elsewhere, but you're given the access to the original. so Changing the data through the pointer affects the original. The problem here is that the Pointer may not point at valid memory. This is a condition that can only be checked at runtime. There is no way the compiler can use to check the validity of a pointer at Compile Time. When you use a Reference, you will get passed the address of the variable to your procedure, and the generated code is identical to passing a pointer. The difference is, that unlike passing pointers, the language syntax and semantics force you to enter the correct variable name. Hope this helps. :)
Bram van Kampen