Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Why do my variables all have address 0 when I use "&"? (http://www.programmingforums.org/showthread.php?t=13257)

Phanixis May 31st, 2007 2:08 PM

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.

Eric the Red May 31st, 2007 2:58 PM

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:

:


int intOne;
int &aRef = intOne;


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.

Phanixis May 31st, 2007 3:35 PM

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(&regressor[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 &regressor[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.

kruptof May 31st, 2007 4:02 PM

Maybe this little tutorial can some what help you with your problem...link

DaWei May 31st, 2007 4:20 PM

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.

lectricpharaoh May 31st, 2007 9:41 PM

See what DaWei says, above. It doesn't look like you're actually wanting to print addresses, so drop the address-of operator (&).
Quote:

Originally Posted by Phanixis
printf("Original Coordinates: %f %f \n", &point_d[0], &point_d[1]);

However, should you want to print addresses, you're still doing it wrong. Use the format specifier "%p" for pointers instead of "%f" (which is for floating-point numbers).

Phanixis Jun 1st, 2007 12:05 PM

%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