Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 31st, 2007, 1:08 PM   #1
Phanixis
Newbie
 
Join Date: May 2007
Posts: 6
Rep Power: 0 Phanixis is on a distinguished road
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.
Phanixis is offline   Reply With Quote
Old May 31st, 2007, 1:58 PM   #2
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
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.
Eric the Red is offline   Reply With Quote
Old May 31st, 2007, 2:35 PM   #3
Phanixis
Newbie
 
Join Date: May 2007
Posts: 6
Rep Power: 0 Phanixis is on a distinguished road
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.
Phanixis is offline   Reply With Quote
Old May 31st, 2007, 3:02 PM   #4
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3 kruptof is on a distinguished road
Maybe this little tutorial can some what help you with your problem...link
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old May 31st, 2007, 3:20 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 31st, 2007, 8:41 PM   #6
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,031
Rep Power: 5 lectricpharaoh will become famous soon enough
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).
__________________
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
lectricpharaoh is offline   Reply With Quote
Old Jun 1st, 2007, 11:05 AM   #7
Phanixis
Newbie
 
Join Date: May 2007
Posts: 6
Rep Power: 0 Phanixis is on a distinguished road
%p fixed the problem. Thanks for the help.
Phanixis is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:32 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC