![]() |
Why do my variables all have address 0 when I use "&"?
Ok, I am having a problem where I am attempting to obtain the address of some variables so I can pass them to a function using pointers. Here is part of my code:
float point_a[2], point_b[2], point_c[2], point_d[2]; point_a[0] = 1.5; point_a[1] = 1.63; point_b[0] = 0.5; point_b[1] = 2.63; point_c[0] = 7.0; point_c[1] = 9.13; point_d[0] = 8.0; point_d[1] = 8.13; printf("Original Coordinates: %f %f \n", &point_a[0], &point_a[1]); printf("Original Coordinates: %f %f \n", &point_b[0], &point_b[1]); printf("Original Coordinates: %f %f \n", &point_c[0], &point_c[1]); printf("Original Coordinates: %f %f \n", &point_d[0], &point_d[1]); When I run this code, printf will print out all zeros for the addresses corresponding to each point. Does C++ not automatically assign addresses to the variables? And if so, how do I get the program to properly assign addresses. Thanks. |
I don't exactly know what you are trying to do. Why do you want to use the '&' operator? You can create a reference to another variable with it. So:
:
Look into how references work. However, if you're just trying to just print the value of the variable, then don't use the '&' operator. |
The reason why I need the addresses of the variables to work is I have another, already working function, that takes the address of the first element of an array as an input, as opposed to simply taking the array as the input.
Specifically: pt_shift(®ressor[0],T,o,xy); Where regressor is a two element array of float variables. However, when I used my own arrays as opposed to the arrays the program was originally using(by using &point_a[0] in place of ®ressor[0]), the function in question was returning nonsense. I nested printf statements within the function and found dereferenced values of the input within the function were completely different from those I passed to the function, even though this was before the function did anything to the input. So I checked to the addresses and found that before I passed the variable to the function, all the addresses were 0. Also, I tried your code example: int test; int &testRef = test; But it didn't work. The error is "missing ';' before '&'. Commenting out "int &testRef=test;" eliminates this error, so I know its not a ";" missing somewhere else in the code. I also get a "'Test Ref' undeclared identifier error. And several others. Thanks for the help. |
Maybe this little tutorial can some what help you with your problem...link
|
Drop the & in the printf statement. Your snippet doesn't call for the use of references (though other code might), so ignore that part for this snippet.
Familiarize yourself with pointers. Explaining errors in a couple of lines of code is pretty much wasted until you do. See the link regarding pointers in my signature. Pay some attention to "What is NOT a pointer". The "equivalence" of pointers and arrays is required by the standard and is not all pervasive. Read the forum's rules/FAQ. The appropriate posting, including the use of code tags, is covered there, and is easier to understand than programming. It's also the polite thing to do. |
See what DaWei says, above. It doesn't look like you're actually wanting to print addresses, so drop the address-of operator (&).
Quote:
|
%p fixed the problem. Thanks for the help.
|
| All times are GMT -5. The time now is 2:30 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC