Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 25th, 2005, 2:06 PM   #11
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
Reference, address. Same thing.
BaroN NighT is offline   Reply With Quote
Old Apr 25th, 2005, 3:38 PM   #12
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>Reference, address. Same thing.
It depends on what you are talking about. In theory, pointers, addresses, and references are all basically the same concept. In C++, a pointer is an address, but a reference has different semantics. Saying that they are the same thing would only confuse the issue more.

>But the & sign in addition to standing for an address, can also be a reference.
Or even bitwise AND.
Eggbert is offline   Reply With Quote
Old Apr 25th, 2005, 7:13 PM   #13
brokenhope
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 126
Rep Power: 4 brokenhope is on a distinguished road
Hmm I think im still having trouble understanding

Is & and * the same thing?

So when you use * it defines it to the address of a variable?

So why do you use it when you declare a variable? Wouldnt you only do it when you define a variables name... like whats the point of doing this char* bloh; ? What does it do diffrently than char bloh; ?

And this is the last question I have, I think it doesnt work but I dont know...

char one = "One";
char two = *one;
char one = "Two";
cout << two;

Would that ouput two? or nothing? or how could I make it output two without drasticaly changing the code? I really dont get this... I thought I did but now I dont...

Last edited by brokenhope; Apr 25th, 2005 at 7:15 PM.
brokenhope is offline   Reply With Quote
Old Apr 25th, 2005, 7:31 PM   #14
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
int * px;  <--declares a pointer of type int (points to an int)

x=1;  <--  just a regular variable

px = &x;  <--assigns the address of x to px's value

now px is assigned the address of x. if you were to
cout<<px<<endl
it would print the same thing as if you were to
cout<<&x<<endl;
it would print the address of x.

if you
cout<<*px<<endl;
it print's the VALUE that px POINTS to, which is x which in this case is 1.

you use the * operator to declare a pointer. that's why a lot of people like to put it next to the type like this:
int* px;

px is the pointer, it is the address of x.

*px is the value pointed to.

&x is the address of x.

the * is called the dereference operator because when you use it on a pointer you "dereference" the pointer which means to get the value of what it is referencing. the & and * symbols are unfortunately used in some different ways which can lead to confusion. as to your pseudo-code

char one = "One";
char two = *one;
char one = "Two";
cout << two;

here is what you want
char one = 'o';
char * two = &one;
one = 't';  //  <--  or *two = 't';  either way
cout<<*two<<endl;

i didn't declare the whole words because you can't do it like you did you have to use an array like

char one[] = "One";

and this will severely complicate this issue for you. i suggest you play with ints or something else right now for messing around with pointers.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.

Last edited by bl00dninja; Apr 25th, 2005 at 7:39 PM.
bl00dninja is offline   Reply With Quote
Old Apr 25th, 2005, 7:38 PM   #15
brokenhope
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 126
Rep Power: 4 brokenhope is on a distinguished road
hmm I think im starting to understand, so what happens in an example where y points to x, and the x variable changes several times, does the y variable change with the x variable? Like

int* y;
int x = 4;
cout << y << endl;
y = &x;
x = x*2;
cout << y << endl;

Would that output:
4
8

or what?
brokenhope is offline   Reply With Quote
Old Apr 25th, 2005, 7:49 PM   #16
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You need to output *y instead of y. Plain ol' vanilla "y" is the address of the variable x; "*y" points to the value of it.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 25th, 2005, 7:53 PM   #17
brokenhope
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 126
Rep Power: 4 brokenhope is on a distinguished road
Ah, now I understand.. when I compile it and try to run it theres nothing on the screen, and a warning error coming up saying that the program encountered a problem and needs to close comes up...

#include <iostream.h>
#include <stdlib.h>

using namespace std;

int main()
{
      int* y;
      int x = 4;
      cout << *y << endl;
      y = &x;
      x = x*2;
      cout << *y << endl;
      system("PAUSE");
      return 0;
}
brokenhope is offline   Reply With Quote
Old Apr 25th, 2005, 7:58 PM   #18
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
#include <iostream.h>
#include <stdlib.h>

using namespace std;

int main()
{
      int* y;
      int x = 4;
      cout << *y << endl; //<--y isn't pointing to anything yet...stray pointer!
      y = &x;                  //<--  move this line up one and you should be good
      x = x*2;
      cout << *y << endl;
      system("PAUSE");
      return 0;
}

now corrected
#include <iostream.h>
#include <stdlib.h>

using namespace std;

int main()
{
      int* y;
      int x = 4;
      y = &x;
      cout << *y << endl;
      x = x*2;
      cout << *y << endl;
      system("PAUSE");
      return 0;
}

tada!!! wasn't that easy?
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Apr 25th, 2005, 8:03 PM   #19
brokenhope
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 126
Rep Power: 4 brokenhope is on a distinguished road
Oh that was just a mistake of not paying attention to where I added the *, thanks a lot I think I finally understand this.
brokenhope is offline   Reply With Quote
Old Apr 29th, 2005, 2:37 AM   #20
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
Talking

feel free to ask more questions in the future.

good luck!
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja 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




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

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