![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2007
Posts: 6
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
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.
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2007
Posts: 6
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
Maybe this little tutorial can some what help you with your problem...link
__________________
Quote:
|
|
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,031
Rep Power: 5
![]() |
See what DaWei says, above. It doesn't look like you're actually wanting to print addresses, so drop the address-of operator (&).
Quote:
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: May 2007
Posts: 6
Rep Power: 0
![]() |
%p fixed the problem. Thanks for the help.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Combining languages | titaniumdecoy | Other Programming Languages | 12 | Jul 13th, 2006 2:03 PM |
| OnlineTextEditor.Com! | Sane | Show Off Your Open Source Projects | 43 | Jun 16th, 2006 8:55 AM |
| Pointers in C (Part II) | Stack Overflow | C | 2 | Apr 29th, 2005 10:39 AM |
| Pointers in C (Part I) | Stack Overflow | C | 4 | Apr 28th, 2005 7:03 PM |
| Beginner question regarding pointers | edd1986 | C | 14 | Apr 13th, 2005 2:55 AM |