Pass by reference
-
I recently was in a little discussion and a question came up. Does a variable that is passed by reference have the same address as it would if it was pointed to? How would I prove or diprove this? Thanks for the help in advance.:confused: Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]
-
I recently was in a little discussion and a question came up. Does a variable that is passed by reference have the same address as it would if it was pointed to? How would I prove or diprove this? Thanks for the help in advance.:confused: Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]
-
I recently was in a little discussion and a question came up. Does a variable that is passed by reference have the same address as it would if it was pointed to? How would I prove or diprove this? Thanks for the help in advance.:confused: Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]
void f(int& v) { printf("var in function: %u\n", &v); } int main() { int v; printf("var in main: %u\n", &v); f(v); }
If these outputs are the same, then the pointers are the same. These HW assignments are fun. Even a monkey could do them. J. ---------------------------- -
I recently was in a little discussion and a question came up. Does a variable that is passed by reference have the same address as it would if it was pointed to? How would I prove or diprove this? Thanks for the help in advance.:confused: Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]
-
Yes they are. To prove it:
BOOL TestAddr(int& ref, int* ptr) { return (DWORD)&ref == (DWORD)ptr; } void main() { int n; assert(TestAddr(n, &n)); }
Thanks this and the other above did the trick. :) Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]
-
void f(int& v) { printf("var in function: %u\n", &v); } int main() { int v; printf("var in main: %u\n", &v); f(v); }
If these outputs are the same, then the pointers are the same. These HW assignments are fun. Even a monkey could do them. J. ----------------------------Thanks, this and the other response were exactly what I was trying to prove. Unfortunately I'm not proficient enough to have done this myself. Thanks again.:) Jeff Patterson Programmers speak in Code. http://www.anti-dmca.org[^]